00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <signal.h>
00022 #include <unistd.h>
00023 #include <e32def.h>
00024 #include <spawn.h>
00025
00026 int gch;
00027 FILE *gfp = NULL;
00028 pid_t chldProcsID;
00029 int gVal = TRUE;
00033 void SIGUSR1_handler(int signum)
00034 {
00035 int ret;
00036 const int delay1 = 1;
00037 if (signum == SIGUSR1)
00038 {
00039 gVal = FALSE;
00040 printf("\nReceived the SIGUSR1 signal from the sigusr2 process\n");
00041 printf("Starting to read from the file\n");
00042 while((gch = fgetc(gfp))!='\n')
00043 {
00044 putchar(gch);
00045 }
00046 printf("\n");
00047 sleep (delay1);
00048
00049
00050 printf("Reading from the file now completed, sending SIGUSR2 to the sigusr2 process\n");
00051
00052
00053 ret = kill(chldProcsID,SIGUSR2);
00054 if(ret)
00055 {
00056 printf("kill() failed to send signal, errno=%d\n",errno);
00057 }
00058 }
00059 }
00060
00074 int main()
00075 {
00076 int ret;
00077 const int delay = 1;
00078 const int size = 50;
00079 const int index = 3;
00080
00081 char *filename = "C:\\asyncFile.txt" ;
00082 char *childProcess = "Z:\\sys\\bin\\sigusr2.exe";
00083 char **argv = (char**)malloc(index*sizeof(char*));
00084 argv[0] = (char*)malloc(size*sizeof(char));
00085 argv[1] = (char*)malloc(index*sizeof(char));
00086 argv[2] = NULL;
00087
00088
00089 strcpy(argv[0], "z:\\sys\\bin\\sigusr2.exe");
00090 sprintf(argv[1],"%d",getpid());
00091
00092
00093 signal(SIGUSR1,SIGUSR1_handler);
00094
00095 printf("*****************************************************************\n");
00096 printf("* Welcome to the asynchronous signal handling demonstration *\n");
00097 printf("*****************************************************************\n");
00098
00099 printf("* This example demonstrates asynchronous signal handling using SIGUSR1 and SIGUSR2 signals.\n");
00100 printf("* The example consists of two processes, the sigusr1 process and the sigusr2 process.\n");
00101 printf("* The sigusr1 process handles the SIGUSR1 signal and sends a SIGUSR2 signal to the sigusr2 process.\n");
00102 printf("* The sigusr2 process handles the SIGUSR2 signal and sends a SIGUSR1 signal to the sigusr1 process.\n");
00103 printf("* The sigusr1 process opens a file and writes some text in it.\n");
00104 printf("* The sigusr1 process then spawns the sigusr2 process and waits until SIGUSR1 is received from it.\n");
00105 printf("* Once the sigusr1 process obtains a SIGUSR1 signal, it starts reading from the file and displays its\ncontent on the console.\n");
00106 printf("* When all the file content is written, the sigusr1 process sends a SIGUSR2 signal to the sigusr2 process and prepares to exit.\n");
00107 printf("* On other side the sigusr2 process keeps waiting for the SIGUSR2 signal from the sigusr1 process.\n");
00108 printf("* On reception of the SIGUSR2 signal, the sigusr2 process prepares to exit.\n");
00109
00110 printf("\nPress the Enter key to continue\n");
00111 getchar();
00112
00113 printf("****************** In the sigusr1 process ********************\n");
00114
00115
00116 printf("\nOpening a file for read and write\n");
00117 if((gfp = fopen(filename,"w+"))!=NULL)
00118 {
00119 printf("Successfully opened the file\n");
00120 fprintf(gfp, "%s", "An asynchronous signal handling example using the SIGUSR1 and SIGUSR2 signals\n");
00121 rewind(gfp);
00122 printf("Writing to the file completed, preparing to start reading from the file\n");
00123
00124 printf("Press the Enter key to spawn the sigusr2 process\n");
00125 getchar();
00126
00127
00128 ret = posix_spawn(&chldProcsID,childProcess,NULL,NULL,argv,(char**)NULL);
00129 if(ret != 0)
00130 {
00131 printf("\n*** failure posix_spawn ***\n");
00132 return EXIT_FAILURE;
00133 }
00134 }
00135
00136 printf("Waiting until SIGUSR1 is obtained from the sigusr2 process");
00137 while(gVal)
00138 {
00139 printf(".");
00140 sleep(delay);
00141 }
00142
00143 if(!fclose(gfp))
00144 {
00145 remove(filename);
00146 gfp=NULL;
00147 printf("The file was closed successfully.\n");
00148 }
00149 else
00150 {
00151 printf("File close failed.\n");
00152 }
00153
00154 printf("Press 'e'+Enter to exit from the sigusr1 process\n");
00155
00156 while((gch=getchar())!= 'e')
00157 {
00158 if(gch == '\n')
00159 continue;
00160 else
00161 printf("The wrong option was selected, please try again!!!\n");
00162 }
00163 return EXIT_SUCCESS;
00164 }