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 #include <string.h>
00026
00027 int gch;
00028 FILE *gfp = NULL;
00029 pid_t chldProcsID;
00030 int gVal = TRUE;
00034 void SIGUSR1_handler(int signum)
00035 {
00036 int ret;
00037 const int delay1 = 1;
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);
00046 }
00047 printf("\n");
00048 sleep (delay1);
00049
00050
00051 printf("Reading from the file now completed, sending SIGUSR2 to the sigusr2 process\n");
00052
00053
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;
00079 const int size = 50;
00080 const int index = 3;
00081
00082 char *filename = "C:\\asyncFile.txt" ;
00083 char *childProcess = "Z:\\sys\\bin\\sigusr2.exe";
00084 char **argv = (char**)malloc(index*sizeof(char*));
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");
00091 sprintf(argv[1],"%d",getpid());
00092
00093
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
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 }