Programming :: Can't Run Second Child In C (Fork)
Apr 10, 2011
Now,I created one parent and two children. I must create some value as random in child1 and child1 has to send these values to child2.Child2 must read them.. now,I cant create some random values and i can send them.But child2 doesnt work. I have two functions. One them is writing, another one is reading. Child2 uses reading function,but it doesnt work (child2 cant call it, because writing function in endless loop) What i must do? I used wait,usleep... No way.. I attached my file and also there is code..
PHP Code: #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/types.h>
[Code].....
View 2 Replies
ADVERTISEMENT
May 23, 2011
I have a doubt about signals in C programming. I have done this little program to explain it. It creates a child process with fork and, when the child ends, receives the SIGCHLD signal and wait for its termination.Ok, quite easy, BUT when I execute this code the SIGCHLD signal is received twice, first as an error (returns -1) and the second one to finish the child process.I don't understand the meaning of the first received signal. Why is it generated? Is the code wrong? (if you add the SIGINT and press Ctrl+C during the execution it also receives two signals instead of one)
Code: #include <stdio.h>
#include <unistd.h>
#include <string.h>
[code]....
View 4 Replies
View Related
Dec 7, 2009
Consider the following code:
Code:
int main()
{
int i=0;
pid_t pid;
for(i=0;i<2;i++)
[code]....
I get the following output:
Parent: chid_pid=4356 i=0 parent's pid=4355
This is child 4356 i=0
This is child 4357 i=1
[code]....
I can observe instead of two children(as I expect) processes there are three. This is because child process 4356 creates its own child. Why all the messages of the type "This is child X i=Y" are concentrated one under another? How exactly fork works? Is affected by the fact that I have a dual-core processor?
View 3 Replies
View Related
May 12, 2011
i got basic knowledge about creating a single child from a parent using fork(). But when it comes into creating multiple children, i am simply stuck. I am trying to create two processes from a parent and it would wait for both two processes to finish. my attempt is as below
[Code]....
View 3 Replies
View Related
Feb 24, 2010
Code:
int main()
{
int pi;
int i=10;
pid=fork();
[code]....
Q 1. The value of the variable pid returned by the fork() function will be greater than 0 in the parent process and equal to zero in the child process? but during forking, there values are exactly copied so what's went wrong here?
Q 2. "changes to the variable in one process is not reflected in the other process" why it is so? >> Even if we have variable i declared as a pointer or a global it wont make a difference.
Code:
int main()
{
int i, pid;
i=10;
printf("before fork i is %d
[code]....
Q. Through this program it is clear that both process is using the data from the same location, so where's the original value is residing when the child process is in execution.?
View 6 Replies
View Related
Sep 16, 2010
As part of my ongoing project I need to put together a messaging server of sorts. So far I have coded a server which accepts multiple connections, which then runs a function that will perform a task, see below.On this sever I have a global structure which is populated using the data found in a read() buffer. However with each fork() that runs the below function each process is handed a blank structure. How could I go about allowing each fork() child process access the same structure?
My code should hopefully show what I am trying to do?
Code: void listen_for_client(int sock)
{
[code]....
View 5 Replies
View Related
Sep 30, 2010
Just coming over from Windows here... where I've been using "CreateProcess" which returns a value depending on whether the process was created successfully or not.
Now I'm trying to create a process on linux and I've been learning about fork/exec, and I've been struggling with the fact that there seems to be no easy way to know (within the original parent process) if exec succeeded or not. (without forcing the parent process to hang around polling it or something).
Anyway, I've now just discovered the posix_spawn function, and it seems exactly what I need. However I'm finding the documentation a little hard to understand. In particular, I can't find actual confirmation that it will definitely return an error if creating the process fails (like CreateProcess does on windows). So can anyone confirm that for me?
The text on the documentation states: "If posix_spawn() or posix_spawnp() fail for any of the reasons that would cause fork() or one of the exec family of functions to fail, an error value shall be returned as described by fork() and exec"
This makes me think it uses exec/fork under the hood, and so I wonder if it can be trusted..?
And also, most of the examples on the internet for creating processes use fork/exec, so I wonder if there is some catch with posix_spawn I haven't read about... (since it seems a much simpler way of doing things)
View 2 Replies
View Related
Aug 19, 2010
I have been struggling with this for about a week now. I'm working with QuickUSB to send video streams to and from a device. I get the fastest results by fork()ing and exec()ing new processes everytime I run a pipeline, which works great when reading from files, but now i have to transition it so that i am reading/writing data over a QuickUSB bus. I have it working for each individual process, but i can't run multiple processes because it won't let me open the device. After looking through QuickUSB's API a little closer, i found that the QHANDLE type that they use to describe the usb device is actually just a usb_dev_handle *. From what i have found, this is an incomplete structure, so i am having trouble with opening it in the main process and passing it to each new process to write to it. I already have semaphores set up to prevent multiple processes using it at the same time. I just CANT find out how to utilize the same usb device between two process that are forked and exec. i also must do the exec() because otherwise my gtk and gstreamer fails for trying to share resources.
View 1 Replies
View Related
Oct 23, 2010
As an assignment i was doing a program to create two process using fork and pass messages between them using message queue.Did it worked well until my friend tried to copy it using scp.suddenly all hell broke loose as processes without ran syncronisation ie. in tech terms the process just wont wait wen a message queue is empty.it keeps on executing randomly.but after a reboot .. everything worked fine. until again i tried to do scp on my system on purpose. and again the program just went mad.
View 3 Replies
View Related
May 25, 2011
I am learning about OS and I wrote this simple forking program... Here's the code..
Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
[Code].....
How can it print the first statement, that is BEFORE the fork() statement twice?
I am running Natty 11.04 btw..
View 8 Replies
View Related
Mar 28, 2011
I have been doing programming since the last 5 years. however, i have used to cut and paste the template without knowing in depth since i am chasing the time to finish a certain project. The title above seems easy if we read the manual, its just that im kinda confuse just for a second. Hope you guys can help me to give a better understanding for me so that i can upgrade my knowledge.Well, during my normal practice cut and paste, i try to understand this code below :-
Code: pid_t pid;
pid=fork();
if (pid < 0) {perror("Erro spawning process : "); exit(EXIT_FAILURE);}
[code]....
View 4 Replies
View Related
Mar 24, 2011
I'm wondering how python's thread & fork support is compared to perl? Which one is best option among Perl and Python
View 1 Replies
View Related
May 13, 2010
I know that fork() copies the address space of the calling process. Say, however, i have a linked list allocated. Will the list be copied over to the child process's space? If so, i would have to free them in the child process as well as the parent process, correct? Or will the variables be copied but not be pointing to any valid address? Or would it just kind of not do anything?example:
Code:
struct ll_ex {
struct ll_ex * next;
[code]....
View 7 Replies
View Related
Nov 24, 2010
I'm trying to write a program that will fork a series of FTP sessions. For each session, there should be separate input and output files associated with stdin and stdout/stderr.
I keep reading how I should be able to do that with dup2() in the child process before the execl(), but it's not working for me. Could someone please explain what I've done wrong? The program also has a 30-second sniper alarm for testing and killing of FTPs that go dormant for too long.
The code: (ftpmon.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
[code]....
The output:
$ ftpmon
Connected to gila-crstest.gilacorp.com (172.16.20.8).
220 (vsFTPd 2.0.1)
ftp> waitpid(): Interrupted system call
Why am I getting the ftp> prompt? If the dup2() works, shouldn't it be taking input from my script and not my terminal? In stead, it does nothing, and winds up getting killed after 30 seconds. The log file is created, but it's empty after the run.
View 3 Replies
View Related
Jan 27, 2011
I have a monitoring program ( GIT link to sourceforge ) which I'm trying to use to track when a child exits/dies/whatever. I'm calling fork(), then close for 0,1,2, and then opening /dev/null, monitored.stdout, and monitored.stderr as a replacement. I'm not sure if I've done something incorrect (perhaps I should use dup2 for explicit assignment?) but it appears that printf() messages are just being blackholed. I've tried setting the line buffering as a last ditch effort. On a different system, using code similar to the spawn_monitor() function, this appears to work fine, which makes me think I'm relying on some implementation specific detail.
Relevant function, for those who don't click links:
Code:
26 int spawn_monitored(char *argv[])
27 {
28 int exit = 0;
29 pid_t child;
30 const struct rlimit inf = {
[code].....
View 14 Replies
View Related
Jul 30, 2009
I am troubleshooting something and I got this problem.
If I do "pstree -p"
It shows,
Code:
[code]....
However, it does NOT show up in "ps -elf"
Code:
ps -elf | grep soffi
0 S whho 7734 1 0 80 0 - 36435 - 11:14 pts/2 00:00:03 /usr/lib/openoffice/program/soffice.bin -splash-pipe=5
0 S whho 7833 7759 0 80 0 - 751 - 11:21 pts/3 00:00:00 grep soffi
I was wondering if 7735, 7736, 7737, 7743 were really processes. Then I checked /proc, I could cd to /proc/7735, /proc/7736, etc, but I could not ls them out. I looked at the man page of "pstree", it says,
Code:
Child threads of a process are found under the parent process and are shown with the process name in curly braces, e.g.
icecast2---13*[{icecast2}]
So, what does all this mean? Does it mean that 7735, 7736, 7737, 7743 are just threads but not processes? If so, why could I cd to /proc/<id> but not see them in "ps -elf".
View 10 Replies
View Related
Aug 18, 2010
I have a root process (on linux) that forks a child and the child process then drops privileges by doing a setuid() to a normal user. After the child setuid()'s, it is of course impossible for it to gain root again by itself. But since the main process is still running as root, i was wondering if there was a simple/smart way of getting the root-master-process to elevate the child back to root (or maybe just to another non-privi uid). Is there some way to do a setuid() on another pid? or maybe something can be done through /proc/<pid>/? Killing the child is not an option (because its what it does today and im trying to find a smarter way). (The program is apache2's mpm-itk worker and the "child" is the actual apache2 process serving a page.)
View 11 Replies
View Related
Sep 6, 2010
I work on Linux for ARM processor for cable modem. There is a tool that I have written (as the job demands) that sends/storms customized UDP packets using raw sockets. I form the packet from scratch so that we have the flexibility to play with different options. This tool is mainly for stress testing routers.
The details are here.
I actually have multiple interfaces created. Each interface will obtain IP addresses using DHCP. This is done in order to make the modem behave as virtual customer premises equipment (vcpe).
When the system comes up, I start those processes that are asked to. Every process that I start will continuously send packets. So process 0 will send packets using interface 0 and so on. Each of these processes that send packets would allow configuration (change in UDP parameters and other options at run time). Thats the reason I decide to have separate processes.
I start these processes using fork and excec from the provisioning processes of the modem.
The problem now is that each process takes up a lot of memory. Starting just 3 such processes, causes the system to crash and reboot.
I have tried the following:- 1-I have always assumed that pushing more code to the Shared Libraries will help. So when I tried moving many functions into shared library and keeping minimum code in the processes, it made no difference to my surprise.
2-I also removed all arrays and made them use the heap. However it made no difference. This maybe because the processes runs continuously and it makes no difference if it is stack or heap?
3-I suspect the process from I where I call the fork is huge and that is the reason for the processes that I make result being huge. I am not sure how else I could go about. say process A is huge -> I start process B by forking and excec. B inherits A's memory area. So now I do this -> A starts C which inturn starts B will also not help as C still inherits A?. I used vfork as an alternative which did not help either. I do wonder why.
reduce the memory used by each independent child processes.
View 1 Replies
View Related
Oct 14, 2010
the wrong part of the forum but basically im working on a project and getting no where what so ever and i was wondering if i could get your help. Basically i have to create two scripts do:
The parent script which is going to:
o spawn several child processes.
o keep track of the progress of the child processes.
[code]...
View 1 Replies
View Related
Jan 6, 2011
I am fairly new to c++. There must be a better way to do the following?:- Say I have a base class, Pet. I have several child classes that extend from this, like Dog, Cat, Fish etc.
I have the following function, that returns a pointer to a new Pet:-
Code: Pet* addPetToVet()
{ //Do some stuff
return new Pet();
} This will return a pointer an instance of a pet object.
Now, if I want to interpret this pet as a dog or cat I have to do this:-
Code: Dog* dogA = static_cast<Dog*>(addPetToVet());
Cat* catA = static_cast<Cat*>(addPetToVet()); Is there a way around this? Casting seems lame. I cant write a function for each type of pet.
View 5 Replies
View Related
Jul 14, 2011
Im using gdb for debugging my application.. I was able to debug child process(when fork comes) .. and in child process we have an exec call to .... So the problem is, when the control come to exec , the exec process is executing at a time... I could not debug the exec. process... error is stack curruption due to same frame So, is there any way to debug the exec process
View 1 Replies
View Related
Mar 28, 2011
I'm writing a sort of toy shell using fork() and execv(). It runs smoothly enough untill the user enters an invalid command at which point the program hangs, so I need a way to check if the program loaded using execv returned correclty or not in the parent process and kill it if it didntI tried writing stderr to a text file to see if something whent wrong but doesnt really work out. Try running ./digenv GDM -i -Q for instance. (-Q is an invalid option for grep which the program runs at a certain point).Heres the code:
Code:
//digenv
//Carl Reg�rdh 2011-03-24
[code]....
View 13 Replies
View Related
Aug 18, 2010
I'm trying to write a shell script that do ftp and download file periodically, this script should be called by a daemon running in the background.
the shell script "script.sh" is as follows:
Code:
yafc ftp://test:test@192.168.1.225:21 < commands
and the "commands" files is
Code:
d Root/md5* /
quit
if I run script.sh it will work just fine. But when the daemon software calls the "script.sh", the script will send ftp login request to the ftp server, but will not even answer the username or anything.
I believe it is something about child process redirection, but I don't know how to deal with it.
This problem is not only with yafc, it is the same with any ftp client or any application like telnet and so.
View 4 Replies
View Related
Oct 13, 2010
If a process forks its child and communicate with the child using pipe, do closing the write end of the pipe and terminating the writing process have the same effect?
View 3 Replies
View Related
Feb 7, 2010
what I am trying to do is to pass an argument to the standard input stream of a child process. I mean I create two programs .. first one invokes the other. second one contains something like
Code:
scanf("%d", &n);
now I want my first program to be able to pass a value to the second one so that it gets stored in n.
View 7 Replies
View Related
May 26, 2010
I'm in the process of writing a program that is a server- it will accept connections and stuff, and spawn a child process for each. However, i've run into a small problem. I do NOT want to bother with keeping track of the processes unless i need to. So, i set SA_NOCLDWAIT (#ifdef) on a SIG_IGN to the SIGCHLD handler through sigaction interface. The standard says that it the kernel will then keep track of reaping zombie processes for me (a HUGE plus). However, upon receiving a SIGINT signal, i want to stop the server from accepting new connections (done), and then wait for there to be no new connections. I was thinking of just putting a loop like so:
Code:
while((wait(NULL) != (pid_t)-1) && errno != ECHILD);
However, I'm not *sure* that this will work, especially with SIGCHLD still ignored. So how can i tell if there are still child processes? I can't find any call like int getnumchld(pid_t proc); (i wish). Plus it would be inefficient to spin on that function anyway. OTOH, i would rather *NOT* have to do the same thing in a loop with a system("ps |...>file"); read(file); etc. either. Is there a way i can portably implement this feature (I was hoping i could run it on linux and the major BSDs, at least).
TO SUM IT UP:
How can i tell if a process has no child processes if i've SIG_IGN'd SA_NOCLDWAIT'd the SIGCHLD? Is there a _reasonably_ portable way to do so? I *don't* want to manually wait for EVERY process. Maybe only those still active at the time of SIGTERM, but that requires keeping track of the number of connections and whether those have terminated...
EDIT: Does anyone know if the above code *would* work, even with SIGCHLD ignored and the kernel cleaning up zombies *for* me? I checked the manpage and it doesn't say much.
EDIT1: Note that all of the processes are in the same process group and session. SO i can find them through this as well. Perhaps even setting the uid/gid and finding all processes run by that group?
EDIT2: i have an idea if the above isn't feasible. If there is no "elegant" way to do it, i could reduce the complexity by sending a SIGUSR1 to the whole process group. Each process would then set a flag telling it to send a SIGUSR1 in reply and send a SIGUSR2 when it is done executing. Then i could keep a count of signals. Maybe that would be *easier*. Or perhaps a count of all child processes and just a termination signal to decrement the counter.
View 2 Replies
View Related
Jun 28, 2010
i need C code to get child pids from its parent pid in linux and in kernel mode, is there anything like getpid() or getppid() which works in kernel mode?
View 2 Replies
View Related
Sep 3, 2010
I'm looking for a way in Perl to be able to take a list of servers, ssh multiple commands to it and store the results. If I do this process serially, sometimes one server will hang the whole script and if it doesn't, it still takes hours to complete.
I'm thinking what I need to do is make a parent loop that calls out a separate process that passes the server name to the child sub process and then executes all the commands I have defined in its own process. If one server 'hangs', at least that won't stop the script from doing all the other servers in the list.
I'm guessing using the fork() command would serve me best, however, all the online descriptions I have found have been vague at best.
View 4 Replies
View Related
Apr 13, 2010
I am writing a perl script where forking a child and the child is waiting for some response from C++ exe. Here is the code snippet:
Code:
my $retPid = fork;
if ($retPid) {
# I am parent ...
# Install signal handler
$SIG{CHLD} = &REAPER; ...
# sleep for a week (6 hours at a time)
for ($cnt = 0; $cnt < 28 ; $cnt++) {
select(undef, undef, undef, 21600);
last if ( ! -e "/proc/$retPid" ); } ...
} else {
# I am child
($iid, $rc, %data) = $cpp_exe->getResponse(604800); # 1 week timeout ... }
If the cpp_exe takes very long time (around 30 hours) to respond then the perl script (both parent and child) is dieing. The SIG{CHLD} handler defined in parent is not getting called. It seems parent is dieing first then child is dieing (or may be both dieing at the same time). Why the script is dieing?
View 1 Replies
View Related
Aug 2, 2010
I am developing a application. In this I fork() 3 childs(lets say child1 , child2, child3) . The parent is now waiting for some input from keyboard.Child3 is continously getting data from child1 and child2 using pipe which it then will print using printf.Now as the parent is waiting for input from user through keyboard while child3 is continously printing the data. I want to do it in different terminals.Can you please guide me how to proceed ahead so that on one terminal , the parent waits for input fromser while on other terminal child3 prints data.
View 1 Replies
View Related