|
Home > Archive > Unix Programming > November 2005 > Trying to do some signal handling ..
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Trying to do some signal handling ..
|
|
| ebrahimbandookwala@gmail.com 2005-11-18, 5:53 pm |
| 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.
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.
3. ANy ideas on how to pass the return_code to the parent process
besides writting it back to a file ??
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 ..
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 2005-11-18, 5:54 pm |
| 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 ***
|
|
|
|
|