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