examples/PIPS/posixsignals/asyncSignal/src/sigusr1.c

00001 /*
00002 Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: This example demonstrates asynchronous signal handling.
00028 */
00029 
00030 
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <signal.h> //The header for signal functionality.
00035 #include <unistd.h> //The header used for getpid().
00036 #include <e32def.h>
00037 #include <spawn.h>
00038 #include <string.h>
00039 
00040 int gch;            // The variable holding a character value, input by the user.
00041 FILE *gfp = NULL;   // The file descriptor variable holding pointer to asyncFile.txt.
00042 pid_t chldProcsID;  // The variable for holding the sigusr2 PID.
00043 int gVal = TRUE;    // Variable used as a boolean value.
00047 void SIGUSR1_handler(int signum)
00048     {
00049     int ret;
00050     const int delay1 = 1;  //Time delay of 1 sec.
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);  //Writing to stdout.
00059             }
00060         printf("\n");  
00061         sleep (delay1);     
00062         
00063         //Sending SIGUSR2 signal to sigusr2 process once the file content is written to the console.
00064         printf("Reading from the file now completed, sending SIGUSR2 to the sigusr2 process\n");
00065         
00066         //Raising SIGUSR2 signal using kill command.
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;                                        //Delay of 1 sec provided to wait for signal.
00092     const int size = 50;                                        //No.of characters allowed in the filename.
00093     const int index = 3;                                        //No.of elements in the array.
00094     
00095     char *filename = "C:\\asyncFile.txt" ;                      //Filename of the file that holds names input by the user.
00096     char *childProcess = "Z:\\sys\\bin\\sigusr2.exe";           //Filename of sigusr2 process.
00097     char **argv = (char**)malloc(index*sizeof(char*));          //Variable holding values to be passed to the sigusr2 process.
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");               //argv[0] holding name of the sigusr2 process.
00104     sprintf(argv[1],"%d",getpid());                             //argv[1] holding PID of sigusr1 process.
00105     
00106     //Setup the custom handler for SIGUSR1.
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         //Spawning sigusr2 process.
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     }

Generated by  doxygen 1.6.2