examples/PIPS/posixsignals/sigtermSignal/src/sigtermSignal.c

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

Generated by  doxygen 1.6.2