diff -r f345bda72bc4 -r 43e37759235e Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/sigterm_signal_8c-source.html --- a/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/sigterm_signal_8c-source.html Tue Mar 30 11:56:28 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,160 +0,0 @@ - -
-00001 // sigtermSignal.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: The source code file for sigtermSignal. -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; //Time delay of 4 sec provided. -00084 const int size = 50; //No.of characters allowed in the filename. -00085 const int index = 3; //No.of elements in the array. -00086 -00087 char *usrProcess = "Z:\\sys\\bin\\raisesignal.exe"; //Filename of process used for raising SIGTERM signal. -00088 char **argv = (char**)malloc(index*sizeof(char*)); //Variable holding values to be passed on to the raiseSignal process. -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"); //argv[0] holding name of the child process. -00095 sprintf(argv[1],"%d",getpid()); //argv[1] holding parent pid. -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+"); //Opening the file for read and write. -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 //Spawning raiseSignal process to accept user input. -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 } -