examples/PIPS/posixsignals/asyncSignal/src/sigusr1.c

00001 // sigusr1.c
00002 //
00003 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 // All rights reserved.
00005 // This component and the accompanying materials are made available
00006 // under the terms of "Eclipse Public License v1.0"
00007 // which accompanies this distribution, and is available
00008 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00009 //
00010 // Initial Contributors:
00011 // Nokia Corporation - initial contribution.
00012 //
00013 // Contributors:
00014 //
00015 // Description:This example demonstrates asynchronous signal handling.
00016 //
00017 
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <signal.h> //The header for signal functionality.
00022 #include <unistd.h> //The header used for getpid().
00023 #include <e32def.h>
00024 #include <spawn.h>
00025 #include <string.h>
00026 
00027 int gch;            // The variable holding a character value, input by the user.
00028 FILE *gfp = NULL;   // The file descriptor variable holding pointer to asyncFile.txt.
00029 pid_t chldProcsID;  // The variable for holding the sigusr2 PID.
00030 int gVal = TRUE;    // Variable used as a boolean value.
00034 void SIGUSR1_handler(int signum)
00035     {
00036     int ret;
00037     const int delay1 = 1;  //Time delay of 1 sec.
00038     if (signum == SIGUSR1)
00039         {
00040         gVal = FALSE;
00041         printf("\nReceived the SIGUSR1 signal from the sigusr2 process\n");
00042         printf("Starting to read from the file\n");
00043         while((gch = fgetc(gfp))!='\n')
00044             {
00045             putchar(gch);  //Writing to stdout.
00046             }
00047         printf("\n");  
00048         sleep (delay1);     
00049         
00050         //Sending SIGUSR2 signal to sigusr2 process once the file content is written to the console.
00051         printf("Reading from the file now completed, sending SIGUSR2 to the sigusr2 process\n");
00052         
00053         //Raising SIGUSR2 signal using kill command.
00054         ret = kill(chldProcsID,SIGUSR2);
00055         if(ret)
00056             {
00057             printf("kill() failed to send signal, errno=%d\n",errno);
00058             }
00059         }
00060     }
00061 
00075 int main()
00076     {
00077     int ret;
00078     const int delay = 1;                                        //Delay of 1 sec provided to wait for signal.
00079     const int size = 50;                                        //No.of characters allowed in the filename.
00080     const int index = 3;                                        //No.of elements in the array.
00081     
00082     char *filename = "C:\\asyncFile.txt" ;                      //Filename of the file that holds names input by the user.
00083     char *childProcess = "Z:\\sys\\bin\\sigusr2.exe";           //Filename of sigusr2 process.
00084     char **argv = (char**)malloc(index*sizeof(char*));          //Variable holding values to be passed to the sigusr2 process.
00085     argv[0] = (char*)malloc(size*sizeof(char));
00086     argv[1] = (char*)malloc(index*sizeof(char));
00087     argv[2] = NULL;
00088 
00089     
00090     strcpy(argv[0], "z:\\sys\\bin\\sigusr2.exe");               //argv[0] holding name of the sigusr2 process.
00091     sprintf(argv[1],"%d",getpid());                             //argv[1] holding PID of sigusr1 process.
00092     
00093     //Setup the custom handler for SIGUSR1.
00094     signal(SIGUSR1,SIGUSR1_handler);
00095     
00096     printf("*****************************************************************\n");
00097     printf("*   Welcome to the asynchronous signal handling demonstration   *\n");
00098     printf("*****************************************************************\n");
00099     
00100     printf("* This example demonstrates asynchronous signal handling using SIGUSR1 and SIGUSR2 signals.\n");
00101     printf("* The example consists of two processes, the sigusr1 process and the sigusr2 process.\n");
00102     printf("* The sigusr1 process handles the SIGUSR1 signal and sends a SIGUSR2 signal to the sigusr2 process.\n");
00103     printf("* The sigusr2 process handles the SIGUSR2 signal and sends a SIGUSR1 signal to the sigusr1 process.\n");
00104     printf("* The sigusr1 process opens a file and writes some text in it.\n");
00105     printf("* The sigusr1 process then spawns the sigusr2 process and waits until SIGUSR1 is received from it.\n");
00106     printf("* Once the sigusr1 process obtains a SIGUSR1 signal, it starts reading from the file and displays its\ncontent on the console.\n");
00107     printf("* When all the file content is written, the sigusr1 process sends a SIGUSR2 signal to the sigusr2 process and prepares to exit.\n");
00108     printf("* On other side the sigusr2 process keeps waiting for the SIGUSR2 signal from the sigusr1 process.\n");
00109     printf("* On reception of the SIGUSR2 signal, the sigusr2 process prepares to exit.\n");
00110     
00111     printf("\nPress the Enter key to continue\n");
00112     getchar();
00113     
00114     printf("****************** In the sigusr1 process ********************\n"); 
00115   
00116          
00117     printf("\nOpening a file for read and write\n");
00118     if((gfp = fopen(filename,"w+"))!=NULL)
00119         {
00120         printf("Successfully opened the file\n");
00121         fprintf(gfp, "%s", "An asynchronous signal handling example using the SIGUSR1 and SIGUSR2 signals\n");
00122         rewind(gfp);
00123         printf("Writing to the file completed, preparing to start reading from the file\n");
00124         
00125         printf("Press the Enter key to spawn the sigusr2 process\n");
00126         getchar();
00127         
00128         //Spawning sigusr2 process.
00129         ret = posix_spawn(&chldProcsID,childProcess,NULL,NULL,argv,(char**)NULL);
00130         if(ret != 0)     
00131             {         
00132             printf("\n*** failure posix_spawn ***\n");        
00133             return EXIT_FAILURE;     
00134             }
00135         }
00136     
00137     printf("Waiting until SIGUSR1 is obtained from the sigusr2 process");
00138     while(gVal)
00139         {
00140         printf(".");
00141         sleep(delay);
00142         }
00143     
00144     if(!fclose(gfp))
00145         {
00146         remove(filename);
00147         gfp=NULL;
00148         printf("The file was closed successfully.\n");
00149         }
00150     else
00151         {
00152         printf("File close failed.\n");
00153         }
00154     
00155     printf("Press 'e'+Enter to exit from the sigusr1 process\n");
00156     
00157     while((gch=getchar())!= 'e')
00158         {
00159         if(gch == '\n')
00160             continue;
00161         else
00162             printf("The wrong option was selected, please try again!!!\n");
00163         }
00164     return EXIT_SUCCESS;
00165     }

Generated by  doxygen 1.6.2