diff -r f345bda72bc4 -r 43e37759235e Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/sigusr1_8c-source.html --- a/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/sigusr1_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 // sigusr1.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:This example demonstrates asynchronous signal handling. -00016 // -00017 -00018 -00019 #include <stdio.h> -00020 #include <stdlib.h> -00021 #include <signal.h> //The header for signal functionality. -00022 #include <unistd.h> //The header used for getpid(). -00023 #include <e32def.h> -00024 #include <spawn.h> -00025 -00026 int gch; // The variable holding a character value, input by the user. -00027 FILE *gfp = NULL; // The file descriptor variable holding pointer to asyncFile.txt. -00028 pid_t chldProcsID; // The variable for holding the sigusr2 PID. -00029 int gVal = TRUE; // Variable used as a boolean value. -00033 void SIGUSR1_handler(int signum) -00034 { -00035 int ret; -00036 const int delay1 = 1; //Time delay of 1 sec. -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); //Writing to stdout. -00045 } -00046 printf("\n"); -00047 sleep (delay1); -00048 -00049 //Sending SIGUSR2 signal to sigusr2 process once the file content is written to the console. -00050 printf("Reading from the file now completed, sending SIGUSR2 to the sigusr2 process\n"); -00051 -00052 //Raising SIGUSR2 signal using kill command. -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; //Delay of 1 sec provided to wait for signal. -00078 const int size = 50; //No.of characters allowed in the filename. -00079 const int index = 3; //No.of elements in the array. -00080 -00081 char *filename = "C:\\asyncFile.txt" ; //Filename of the file that holds names input by the user. -00082 char *childProcess = "Z:\\sys\\bin\\sigusr2.exe"; //Filename of sigusr2 process. -00083 char **argv = (char**)malloc(index*sizeof(char*)); //Variable holding values to be passed to the sigusr2 process. -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"); //argv[0] holding name of the sigusr2 process. -00090 sprintf(argv[1],"%d",getpid()); //argv[1] holding PID of sigusr1 process. -00091 -00092 //Setup the custom handler for SIGUSR1. -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 //Spawning sigusr2 process. -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 } -