Re: Trying to do some signal handling ..

From: Barry Margolin (barmar_at_alum.mit.edu)
Date: 11/18/05


Date: Fri, 18 Nov 2005 14:47:40 -0500

In article <1132327732.777851.163990@g43g2000cwa.googlegroups.com>,
 ebrahimbandookwala@gmail.com wrote:

> Hi , I am trying to do simple stuff with signals rather unsuccessfully
> !! . ( 2nd code )
>
> 1.In my main im trying to catch SIGUSR1 & 2 , but when I do
> kill -USR1 <proc_id>
> from the prompt these signals never get caught by my program for some
> reason.

I'm not sure.

>
> 2. I am trying to write the value of return_code in the ckfile() or
> even print it to screen but neither of them work.

When execl() is successful, it never returns, because the process's
program is replaced with the program you exec.

>
> 3. ANy ideas on how to pass the return_code to the parent process
> besides writting it back to a file ??

The exit status is returned by wait() in the parent process. If you
want some other communication between the child process and parent, you
can use shared memory or a pipe.

>
> Thx .. all
>
>
> Also I am trying to get the time of the last fork () call by using
> process accounting but I cant read the values from the accounting file
> for some reason ..

When you call acct(NULL), you're turning off accounting. I don't see
any place where you turned it on. And also, this function can only be
used by the super-user.

>
> heres the code for that ..
>
> ------------------------------------------------------------------------------
> -------------------------------------------------------
> #include <stdio.h>
> #include <sys/types.h>
> #include <sys/acct.h>
>
> #define ACCTFILE "./accounting.txt"
> static unsigned long compt2ulong(comp_t);
>
> int main()
> {
> struct acct acdata;
> FILE * fp;
>
> acct((char *)NULL);
> if( (fp=fopen(ACCTFILE,"r")) == NULL )
> {
> perror("Cannot open File!!" );
> }
> while(fread(&acdata,sizeof(acdata),1,fp)==1)
> {
> printf("%s\t",acdata.ac_comm);
> printf("%3u\n",compt2ulong(acdata.ac_btime));
> }
> fclose(fp);
> return 0;
> }
>
> static unsigned long compt2ulong(comp_t comptime)
> {
> unsigned long val;
> int exp;
>
> val = comptime & 017777;
> exp = (comptime >> 13) & 7;
> while(exp-- > 0 )
> val *= 8;
> return val;
> }
> ------------------------------------------------------------------------------
> -------------------------------------------------------
> #define _INCLUDE_POSIX_SOURCE
> #include <sys/types.h>
> #include <fcntl.h>
> #include <unistd.h>
> #include <dirent.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/stat.h>
> #include <signal.h>
> #include <sys/wait.h>
>
> int gpid;
>
> void dispDirFiles(char * path)
> {
>
> DIR * mydir;
> struct dirent * mydirent;
> struct stat statbuf;
>
> mydir = opendir(path);
> if(!mydir)
> return;
>
> while(1)
> {
> mydirent = readdir(mydir);
> if(!mydirent)
> break;
> if(stat(mydirent->d_name,&statbuf) != 0)
> return;
> if(S_ISREG(statbuf.st_mode))
> {
> printf("\n\t%s\n",mydirent->d_name);
> }
> }
>
> if(closedir(mydir))
> {
> perror("Directory did not close");
> }
> }
>
> void readfile(char * path , char * filename)
> {
> char pfname[100];
> char * args[4];
>
> pfname[0] = '\0';
> strcat(pfname,path);
> strcat(pfname,filename);
>
>
> //printf("%s",pfname);
>
> args[0] = "/usr/bin/od";
> args[1] = "-xc";
> args[2] = pfname;
> args[3] = '\0';
>
> if(execvp(args[0],args) < 0)
> {
> perror("Exec Error");
> }
> }
>
>
> int ckfile( char * path , char * filename)
> {
> char pfname[100];
> FILE * fp , *file;
> char * checksum;
> char execstr[100];
> int return_code;
>
>
> //filedes = open("temp.out" , O_WRONLY | O_CREAT );
> //dup2(1,filedes); // duplicate file desc
>
> file = fopen ("return.cod" , "w" );
>
> checksum = getenv("CHECKSUM");
> execstr[0]='\0';
>
> strcat(execstr,"/usr/bin/");
> strcat(execstr, checksum);
>
>
> pfname[0] = '\0';
> strcat(pfname,path);
> strcat(pfname,filename);
>
> fp = freopen("temp.out" , "a+" , stdout);
> return_code = execl(execstr , checksum, pfname , (char *)NULL);
>
> if( return_code < 0)
> {
> perror("Exec Error");
> }
> //close(filedes);
> printf("\nChild:Return Code %d" , return_code);
>
> fprintf(file , "%d" , &return_code);
>
> fclose(file);
> fclose(fp);
> return return_code;
> }
>
> void doSomething(void)
> {
> int status;
> printf("\nChild: Sleeping for 1s\n");
> sleep(1);
> return;
> }
>
> void doSomethingNew(void)
> {
> int status;
> printf("\nUSR: Sleeping for 1s\n");
> sleep(1);
> return;
> }
>
> int main()
> {
> char pathname[100];
> char filename[100];
> pid_t pid;
> int status=-2;
> int st;
> int choice=5;
>
> struct sigaction act;
>
>
> act.sa_handler = SIG_IGN;
> sigemptyset(&act.sa_mask);
> act.sa_flags=0;
>
> sigaction(SIGINT, &act , NULL);
>
> act.sa_handler = doSomething;
> sigaction(SIGCHLD , &act , NULL);
>
> act.sa_handler = doSomethingNew;
> sigaction(SIGUSR1,&act , NULL);
> sigaction(SIGUSR2,&act , NULL);
>
> printf("Enter Path ");
> gets(pathname);
>
>
> while(choice !=4)
> {
>
> printf("\n\n\t\tReturn Code from the prev Execution of
> Sum :
> %d\n\n",status);
>
>
> printf("\n\n\t1.List\n\t2.Read\n\t3.Sum\n\t4.Exit\n\t\tfilem");
> scanf("%d",&choice);
>
> //printf("%d",status);
> //status = -2;
>
> switch(choice)
> {
> case 1:
> dispDirFiles(pathname);
> break;
>
> case 2:
> //sleep(1);
> printf("\nEnter File ");
> scanf("%s",filename);
> if( (pid=fork())<0 )
> {
> perror("Fork Error!!!");
> }
> else if(pid==(pid_t)0)
> {
>
> readfile(pathname,filename);
>
> }
> else
> {
> printf("\nParent:Waiting
> FOR CHILD!!!\n");
> if( wait(&st) != pid )
> perror("\nWait Error !! ..");
>
> printf("\nParent:Done!!\n");
> }
> //while(getchar()!='.');
> break;
>
> case 3:
> //sleep(1);
> printf("\nEnter File ");
> scanf("%s",filename);
>
> if( (pid=fork())<0 )
> {
> perror("Fork Error!!!");
> }
> else if(pid==(pid_t)0)
> {
> status =
> ckfile(pathname,filename);
> //printf("Status
> :",status);
> }
> else
> {
> printf("\nParent:Not
> Waiting\nParent:Finished\n");
> //if( wait(&status) !=
> pid )
> // perror("Wait
> Error !! ..");
>
> }
>
> break;
> case 4:
> break;
>
> }
>
> printf("\nPress . to continue...");
> while(getchar() != '.' );
> }
>
>
>
>
>
> return 0;
> }

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


Relevant Pages

  • Re: C Programming
    ... > reason for why it matters. ... There are books around that carefully do not teach bad practice, ... this book doesn't use void main. ... is int mainor int main. ...
    (Fedora)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)
  • Re: Function prototype vs implementation mismatch in C++
    ... there would be to inform the user of the API (header file). ... void display(const int); ... reason a slightly different name might be more appropriate. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Should function argument be changed in function body?
    ... > I don't think that's a good reason. ... > int foo1; ... to return void. ... because that leads to such ugly foulups as: ...
    (comp.lang.c)
  • Re: why is perl -e unlink(glob("*")) so much faster than rm ?
    ... so on a hunch I decided to try this to see it I ... in the parent process), only one of which is related to the actual unlink. ... One possible reason is if that gives you an argument ... when I ran the rm command on an idle server it was much faster. ...
    (comp.lang.perl.misc)