Sponsored Content
Top Forums Programming Fork and then exec problem with signals Post 302460579 by sushil_shalin on Thursday 7th of October 2010 05:08:01 AM
Old 10-07-2010
Yes, Find below the code.. Smilie

Code:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
pid_t pid;
void sigHandler(int signo)
{
  printf("[%d]signal received \n",getpid());
  pid = fork();
  if(pid  ==  0)
  {
    printf("[%d] I m child process \n",getpid());
    signal(SIGHUP,sigHandler);
    execl("./chainNew", "chainNew", (char *)0);
  }
  else if(pid > 0)
  {
    printf("[%d] I m Parent process \n",getpid());
    sleep(60*10);
    exit(0);
  }
  else
  {
    printf("[%d]  Fork Failed \n",getpid());
  }
}
int main ()
{
  signal(SIGHUP,sigHandler);
  signal (SIGCHLD, SIG_IGN);
  while (1)
  {
    printf("[%d] Waiting for SIGHUP \n",getpid());
    sleep(2);
  }
}

 

10 More Discussions You Might Find Interesting

1. Programming

Fork and exec

Hello! I am working on a server where I should have 4 (resident)processes, one of them being "the father" of the others, so I do 3 forks. The problem that I have is that I do an accept (for sockets) in the "father" process and I want to transmit the job to one of the processes "child" with... (3 Replies)
Discussion started by: driki
3 Replies

2. Programming

Problem with signals - 3 process communication

Hello, I would like to ask you for a little help with program I'm working on. I have problems with signals and synchronizing processes (I'm quite new to this part of programming). Process "parent" creates new child process "child1" and this process creates new child process "child2". The... (2 Replies)
Discussion started by: Nightwright
2 Replies

3. UNIX for Dummies Questions & Answers

FORK/EXEC technique

Hi! Can someone explain me exactly this technique? Why a process (PARENT) creates a copy of itself with FORK (CHILD)? What's the reason of this behaviour? Sorry, but I cannot understand the logic behind it. Thanks. (4 Replies)
Discussion started by: marshmallow
4 Replies

4. Programming

fork/exec clobbers write. I need ideas why...

On my *nix box, I have a telegram program. When I go like tel person "la\nla\nla\n" the person sees "la\nla\nla\n" However, when I have a program that forks and execs tel like: pid = fork(); if (pid < 0) { perror("fork failed"); exit(EXIT_FAILURE); } if (pid == 0) {... (7 Replies)
Discussion started by: frequency8
7 Replies

5. Solaris

fork and exec ftp

Hi, I need to find/implement an application that FTPs (puts) all new files in a certain directory to an external storage unit. This application should check for new files every 10 seconds (leaving the FTP connection open in between the 10 seconds). The easiest way would be if there are... (2 Replies)
Discussion started by: KittyJ
2 Replies

6. Shell Programming and Scripting

fork and exec

I need to ssh to a remote server and run my script there. This is my script. $ssh = "ssh username@host"; $cmd = "$ssh 'cd <my dir> && < sudo Run_exe>'"; my $pid = fork; if ($pid == 0){ exec $cmd; } When I run this I get: pccons_getchar: got r == 0 (1 Reply)
Discussion started by: looza
1 Replies

7. Programming

How forbid use fork() in exec() program.

Hello World! I am writing code in C++ which have to launch another application X using exec(). I would like to set some limits on it using setrlimit etc... My problem is that i don't know how to forbid using fork() and strlimit by application X. How can i do it? (3 Replies)
Discussion started by: kzi
3 Replies

8. Programming

Newbie question on exec,fork, wait,pipe C

Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine. So, i want to... (1 Reply)
Discussion started by: Cuervo
1 Replies

9. UNIX for Dummies Questions & Answers

fork with exec

What is is difference between 'fork with exec' and 'fork without exec'? How both are related? (1 Reply)
Discussion started by: kkalyan
1 Replies

10. UNIX for Beginners Questions & Answers

Question about global environment variables & fork() exec()

Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)
Discussion started by: bodisha
2 Replies
vfork(2)							   System Calls 							  vfork(2)

NAME
vfork, vforkx - spawn new process in a virtual memory efficient way SYNOPSIS
#include <unistd.h> pid_t vfork(void); #include <sys/fork.h> pid_t vforkx(int flags); DESCRIPTION
The vfork() and vforkx() functions create a new process without fully copying the address space of the old process. These functions are useful in instances where the purpose of a fork(2) operation is to create a new system context for an execve() operation (see exec(2)). Unlike with the fork() function, the child process borrows the parent's memory and thread of control until a call to execve() or an exit (either abnormally or by a call to _exit() (see exit(2)). Any modification made during this time to any part of memory in the child process is reflected in the parent process on return from vfork() or vforkx(). The parent process is suspended while the child is using its resources. In a multithreaded application, vfork() and vforkx() borrow only the thread of control that called vfork() or vforkx() in the parent; that is, the child contains only one thread. The use of vfork() or vforkx() in multithreaded applications, however, is unsafe due to race condi- tions that can cause the child process to become deadlocked and consequently block both the child and parent process from execution indefi- nitely. The vfork() and vforkx() functions can normally be used the same way as fork() and forkx(), respectively. The calling procedure, however, should not return while running in the child's context, since the eventual return from vfork() or vforkx() in the parent would be to a stack frame that no longer exists. The _exit() function should be used in favor of exit(3C) if unable to perform an execve() operation, since exit() will invoke all functions registered by atexit(3C) and will flush and close standard I/O channels, thereby corrupting the par- ent process's standard I/O data structures. Care must be taken in the child process not to modify any global or local data that affects the behavior of the parent process on return from vfork() or vforkx(), unless such an effect is intentional. Unlike fork() and forkx(), fork handlers are not run when vfork() and vforkx() are called. The vfork() and vforkx() functions are deprecated. Their sole legitimate use as a prelude to an immediate call to a function from the exec family can be achieved safely by posix_spawn(3C) or posix_spawnp(3C). Fork Extensions The vforkx() function accepts a flags argument consisting of a bitwise inclusive-OR of zero or more of the following flags, which are defined in the header <sys/fork.h>: FORK_NOSIGCHLD FORK_WAITPID See fork(2) for descriptions of these flags. If the flags argument is 0, vforkx() is identical to vfork(). RETURN VALUES
Upon successful completion, vfork() and vforkx() return 0 to the child process and returns the process ID of the child process to the par- ent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error. ERRORS
The vfork() and vforkx() functions will fail if: EAGAIN The system-imposed limit on the total number of processes under execution (either system-quality or by a single user) would be exceeded. This limit is determined when the system is generated. ENOMEM There is insufficient swap space for the new process. The vforkx() function will fail if: EINVAL The flags argument is invalid. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Obsolete | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
exec(2), exit(2), fork(2), ioctl(2), atexit(3C), exit(3C), posix_spawn(3C), posix_spawnp(3C), signal.h(3HEAD), wait(3C), attributes(5), standards(5) NOTES
To avoid a possible deadlock situation, processes that are children in the middle of a vfork() or vforkx() are never sent SIGTTOU or SIGT- TIN signals; rather, output or ioctls are allowed and input attempts result in an EOF indication. To forestall parent memory corruption due to race conditions with signal handling, vfork() and vforkx() treat signal handlers in the child process in the same manner as the exec(2) functions: signals set to be caught by the parent process are set to the default action (SIG_DFL) in the child process (see signal.h(3HEAD)). Any attempt to set a signal handler in the child before execve() to anything other than SIG_DFL or SIG_IGN is disallowed and results in setting the handler to SIG_DFL. On some systems, the implementation of vfork() and vforkx() cause the parent to inherit register values from the child. This can create problems for certain optimizing compilers if <unistd.h> is not included in the source calling vfork() or if <sys/fork.h> is not included in the source calling vforkx(). SunOS 5.11 13 Dec 2006 vfork(2)
All times are GMT -4. The time now is 06:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy