00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <unistd.h>
00021 #include <sys/types.h>
00022 #include <signal.h>
00023 #include <e32def.h>
00024 #include <time.h>
00025
00026
00027 FILE *gfp = NULL;
00028 char *testfile = "C:\\testfile.txt";
00029
00033 void PressKey()
00034 {
00035 int ch;
00036 printf("Press 'e'+Enter to exit\n");
00037 while((ch=getchar())!= 'e')
00038 {
00039 if(ch == '\n')
00040 continue;
00041 else
00042 printf("wrong option selected, please try again!!!\n");
00043 }
00044 }
00051 void SIGTERM_handler(int signum)
00052 {
00053 if(signum == SIGTERM)
00054 {
00055 if(gfp != NULL)
00056 {
00057 printf("Received the SIGTERM signal from the raiseSignal process\n");
00058 fclose(gfp);
00059 remove(testfile);
00060 gfp = NULL;
00061 printf("The open file descriptor was closed\n");
00062 PressKey();
00063 exit (0);
00064 }
00065 }
00066 }
00067
00077 int main()
00078 {
00079 char input[11];
00080 int count = 0,num;
00081 int ch,ret;
00082 pid_t usrProcsID;
00083 const int delay = 4;
00084 const int size = 50;
00085 const int index = 3;
00086
00087 char *usrProcess = "Z:\\sys\\bin\\raisesignal.exe";
00088 char **argv = (char**)malloc(index*sizeof(char*));
00089 argv[0] = (char*)malloc(size*sizeof(char));
00090 argv[1] = (char*)malloc(index*sizeof(char));
00091 argv[2] = NULL;
00092
00093
00094 strcpy(argv[0], "z:\\sys\\bin\\raisesignal.exe");
00095 sprintf(argv[1],"%d",getpid());
00096
00097 printf("*****************************************************************************************\n");
00098 printf("* Welcome to the demonstration of a graceful process termination using a SIGTERM signal *\n");
00099 printf("*****************************************************************************************\n");
00100
00101 printf("* This example shows how a SIGTERM signal can be used to gracefully terminate a process.\n");
00102 printf("* The example first creates a process called sigtermSignal, assigns a custom handler\nfor the SIGTERM signal, opens a file and gets names from the user to be written in to it.\n");
00103 printf("* It then simultaneously spawns a raiseSignal process and starts reading from the file.\n");
00104 printf("* The raiseSignal process sends a SIGTERM signal to the sigtermSignal process.\n");
00105 printf("* On receiving the SIGTERM signal, the sigtermSignal process closes all the \nopen file descriptors and prepares to exit.\n");
00106
00107 printf("\nPress the Enter key to continue\n");
00108 getchar();
00109
00110 signal(SIGTERM, SIGTERM_handler);
00111 printf("************ In the sigtermSignal process ************\n");
00112 printf("Enter the number of names you want to enter (e.g. 8) :\n");
00113 scanf("%d", &count);
00114 gfp=fopen(testfile, "w+");
00115 if(gfp != NULL)
00116 {
00117 printf("File open sucessful\n");
00118 }
00119 else
00120 {
00121 printf("File open failed\n");
00122 }
00123 for(num=1; num<=count; num++)
00124 {
00125 printf("Name (%d)=", num);
00126 scanf("%s", input);
00127 fprintf(gfp, "%s\n", input);
00128 }
00129
00130 printf("Press the Enter key to spawn the raiseSignal process\n");
00131 getchar();
00132 getchar();
00133
00134 ret = posix_spawn(&usrProcsID,usrProcess,NULL,NULL,argv,(char**)NULL);
00135 if(ret != 0)
00136 {
00137 printf("\n*** failure posix_spawn ***\n");
00138 return EXIT_FAILURE;
00139 }
00140 rewind(gfp);
00141 printf("Starting to read from the file\n");
00142 for(num=1; num<=count; num++)
00143 {
00144 sleep (delay);
00145 printf("Name (%d)=", num);
00146 while((ch=(fgetc(gfp))) != '\n')
00147 {
00148 putchar(ch);
00149 }
00150 printf("\n");
00151
00152 }
00153 if(!fclose(gfp))
00154 {
00155 remove(testfile);
00156 gfp = NULL;
00157 printf("File closed successfully\n");
00158 }
00159 else
00160 {
00161 printf("File close failed\n");
00162 }
00163
00164 PressKey();
00165 return EXIT_SUCCESS;
00166 }