Alright so I am getting a weird problem with the following code int toDB[2]; int fromDB[2]; char * param1; char * param2; err = pipe(toDB); if(err==-1) { printf("Error on pipe creation: %d\n", errno); exit(1); } sprintf(param1, "%d", toDB[0]); err = pipe(fromDB); if(err==-1) { printf("Error on pipe creation: %d\n", errno); exit(1); } sprintf(param2, "%d", fromDB[1]); The last line of code `sprintf(param2, "%d", fromDB[1]);` is the line causing the problem, if it is commented out the code runs, if it is not commented out I get a core dump, can anyone see what i am doing wrong? NOTE: This is not the …

Member Avatar
Member Avatar
+0 forum 5

How do I use pipes to communicate with another proccess that is being run using an execl command? I know how I can send info to that process but how do I get info back from that proccess? Some more information on what Im trying to do: I am trying to write a small database program. There are two programs, one program is an interface program where one will write the commands, the other program, the database program, will take in the commands and process them. the interface program has a parent and a child process. The child is the …

Member Avatar
Member Avatar
+0 forum 4

Hi, I have to create a program that creates N child processes, and they muliply the matrix. I have some base codes but when I try to run them they give me segmentation error and I cant find the problem. #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <string.h> #define BASEPID 9000 #define MAX_SIZE 1000 int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE]; typedef struct { long priority; int index; int finished; int result[MAX_SIZE]; } matrix; int main(int argc, char *argv[]) { int size = atoi(argv[1]), rows, cols, z, PID, proc, parent = 1, inbox, outbox, mailbox, …

Member Avatar
Member Avatar
+0 forum 3

Hello. I need to set up some pipes for a homework assignment as part of a bigger project. The issue I'm having is that the array (args) I'm using to populate the pipe arguments keeps segfaulting and I don't know why. Everything has been malloced beforehand and the faults only occur at the mentioned places. Below, the relevant code: char** args; char** pipe1_args; int pipe1_args_size; char** pipe2_args; int pipe2_args_size; while (temp_std_command && array_size <= 255) //take args and break them up into an array { args[i] = malloc(sizeof(char)*255); strcpy(args[i], temp_std_command); temp_std_command = strtok(NULL, " "); i++; array_size++; } i++; array_size++; …

Member Avatar
Member Avatar
+0 forum 2

Hi members, This is a program i wanted to implement semaphore between parent and child processes which are trying to access a shared variable called counter ... i dont know why it is not getting incremented during the child process !! someone help please !! #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <semaphore.h> #include <stdlib.h> #include <errno.h> #include <string.h> sem_t mutex; int counter; int main () pid_t child_pid; sem_init(&mutex, 0, 1); printf ("the main program process ID is %d\n", (int) getpid ()); child_pid = fork (); if (child_pid != 0) { printf ("This is the parent process, with id %d\n", …

Member Avatar
Member Avatar
+0 forum 2

Hi ya guys, I have been learning about forks and processes, below is my code that i have created but am having difficulty on certain things. **1. Add code to print out and note the values of :** i. The values held within the array fd[ ] from the parent and child processes. ii.The process id’s of the parent and child processes. () **2. From within the child process, create another fork().** i. What is the overall structure of the processes afterwards? ii. What happens to the original child process id? #include <stdio.h> #include <unistd.h> int main() { int n; …

Member Avatar
+0 forum 0

Here is a code that creates two childern using fork. My question is how can we create mutiple grandchildern and great grandchildern at the same time . And the line 21 of code gives an unexpected output i.e instead on one parent id it gives three id's. Here is the output :- This is a child1 process and pid is 3271 This is child2 process and pid is 3272 The parent process has pid 2206 The parent process has pid 3270 The parent process has pid 1. #include<stdio.h> main() { int child1,child2; int i; child1 = fork(); if(child1==0) { printf("This …

Member Avatar
Member Avatar
+0 forum 1

Hello, I have a course that is Operational Systems.. My lecturer doesnt explain so much and after He say, You must solve this problem, because u r a programmer.. I have such a problem with linux programming. I put my code here, He wants, I must allow multi-client to server. It means, server must accept all clients that request from multiple terminal..And also server can send value which clients connected And I must use fork. I tried some ways but no successful. And also he said, you must edit my server codes, you cant own code. Can u please tell …

Member Avatar
+0 forum 0

Hi All, In my script, i am trying to call multiple fork call. if i call less than 10 times, its working fine. If i call more than 20 or 30 times, most of the process getting time out or died foreach my $ttyport (@processTTY) { my $pid = fork();Inline Code Example Here .............. .............. .............. } Can somebody suggest the solution for this issue? `

Member Avatar
Member Avatar
+0 forum 1

So i'm trying to make multiple clients logon to a server through filesockets, and perform simple commands like ls -l, ls, mkdir. I've managed to make one client recieve output of ls -l command, sometimes, its buggy. Now i've been sitting for days trying to figure out how to fork() correctly to handle multiple clients, and what sockets to use, close and so on. Perhaps someone can shed some light? To make it work the client program must be in the same directory as the server program (did not know how to make a path from different folder of client …

Member Avatar
Member Avatar
+0 forum 2

hi friends I've a code to compute integral with area function in code. I used Fork, and for returned values I must use shared memory. now I need to embed a semaphore in my code to control the critical area(here is shared memory). I don't know how to embed semaphore in it. Here is the code: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> #include<sys/types.h> #include<sys/shm.h> #include<semaphore.h> int area(int a,int b,int H) {return (((a*a)+(b*b))*H)/2;} sem_t sem; main() { int *value; pid_t pid; int i; int status; int result=0; int pids[10]; int shmid; key_t key; key=ftok("prog.c",0); shmid=shmget(key,1024,0644|IPC_CREAT); value=shmat(shmid,(void *)0,0); if(value==(int *)(-1)) perror("shmat"); if(sem_init(&sem,0,0)==-1) perror("Semaphore initialize …

Member Avatar
Member Avatar
+0 forum 1

Okay I am trying to write a program where a parent process creates three child processes. Also waits for each of them to finish. Here is my code: [code] #include <stdio.h> #include <unistd.h> main() { int i; // create 3 child processes for (i = 0; i < 3; i++) { if (fork() == 0) { printf("Child process no.%d with ID: %d created from parent process\n", i); break; } } // wait for each child process to finish } [/code] There is one error line "if (fork() == 0) {" What is wrong with that? I tried to run it …

Member Avatar
Member Avatar
+0 forum 6

**My academic assignmentt ** **Please help me in coding for this assignment** In Linux Ubuntu flavor, write a program in C language to do the following tasks. 1) Use fork ( ) system call to create a child process. 2) Child process should first take student name as input and then it should display student name that it takes from user. At last, child process should display numbers from 0 to 5. 3) Parent process should first take Student ID as input and then it should display Student ID that it takes from user. At last, parent process should display …

Member Avatar
Member Avatar
+0 forum 3

I'm applying the peterson solution for protecting memory of the critcal section. Each child process should contain a local variable that keeps track of the number of times it interrupts the other child while it is in its critical section. The process should display this value before terminating. I'm having trouble counting the number of interrupts for these 2 child procsses. When i run the whole program it prints out 0 which I can expect to happen sometimes but not all the time. I figured I could use flag or turn to clarify if a child process is waiting to …

Member Avatar
Member Avatar
+0 forum 1

If a child process encounters exit(0) statement, where is this exit status returned in the parent process? Is there any variable associated with every process that holds this information? If not, how do children and parent processes communicate?

Member Avatar
Member Avatar
+0 forum 2

Can somone please explain to me what does fork() != 0 mean? From what I understand I think it means if fork is not false? Or if fork is true then.... I dont understand how Fork() can be true or false, seeing that it just creates a copy of a process into a parent and child. 1 #include "csapp.h" 2 3 int main() 4 { 5 int x = 3; 6 7 if (Fork() != 0) 8 printf("x=%d\n", ++x); 9 10 printf("x=%d\n", --x); 11 exit(0); 12 } EDIT: Also if a program where to say "if (Fork() == 0)" what …

Member Avatar
Member Avatar
+0 forum 3

what does fork() do in c.? Is it same as fork operation in OS ? What is the o/p of following program ? #include<stdio.h> main() { int i=0; fork(); printf("%d",i++); fork(); printf("%d",i++); fork(); wait(); }

Member Avatar
Member Avatar
+0 forum 2

I was asked to write a program, based on parent-child cooperation, which uses a “parallel” merge sort to a sequence of 800 numbers from a text file.I have to read the sequence of 800 integers, which I store in an array, and then pass half of them to a child, and the other half to the second child of the parent class. I have to do this until only 100 numbers are left in each child. When I have reached this the numbers must be sorted, which I am doing using a bubble sort.Once these are sorted the must be …

Member Avatar
Member Avatar
+0 forum 7

I have written a code for checking vfork in unix. But I am getting the ambiguous behavious. When I am using exit(0) in child process , output is fine i.e; output :- Before fork I am a child My name is parent child PID=> 23133 kValue => 2 ] But when I am using "[B]return 0[/B]" in place of [B]exit(0)[/B] , program keeps on executing and each time a new process is created infinitely. output :- Before fork I am a child My name is parent child PID=> 23143 kValue => 2 Before fork I am a child My name …

Member Avatar
Member Avatar
+0 forum 4

I'm using sockets in C to write 10 bytes to a socket and then read 10 bytes from that socket. I create a parent process that forks, creating a child that acts as the socket server that writes the 10 bytes. After the 10 bytes are wrote, back in the parent, those 10 bytes get read from the socket. This is what is supposed to happen. My current output just shows the server "waiting". I'm not sure what's causing the hangup between the parent/child but any help or suggestions would be much appreciated. Here's my code below. [CODE] #include <stdlib.h> …

Member Avatar
Member Avatar
+0 forum 8

Hello there, I'm new to C programming and am following a course in C. I've got an example code for piping. It's not a very hard code to understand. It goes like this: [CODE] #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #define READ_END 0 #define WRITE_END 1 const char banner [] = "hello there\n"; int main() { int pipe_ends[2]; pid_t pid; if(pipe(pipe_ends)) { printf("Could not create pipe\n"); return -1; } pid = fork(); if(pid < 0) { printf("Fork failed\n"); return -1; } if(pid > 0) { /* parent */ int i; close(pipe_ends[READ_END]); for(i=0; i<10; i++) { printf("Parent Writing [%d]...\n",i); …

Member Avatar
Member Avatar
+0 forum 3

Hi everybody! I am trying to use fork() and wait to perform a simple task with c++ but I have noticed that the performance is quite different from what I expected. [CODE] #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int number = atoi(argv[1]); int i; int pid; pid=fork(); if (pid!=0){ wait(); printf ("hi!"); } else { sleep (3); int i; printf("I am the child\n"); printf("my pid=%d\n", getpid()); printf("and my parent pid=%d\n", getppid()); } } [/CODE] This code should create a child process and the parent waits for the child to end to print "hi!". If …

Member Avatar
Member Avatar
+0 forum 3

Hi everyone. i have an issue with a server program i'm developing for a university project. The issue is the following: The server program i'm building uses shared memory and forks. However i have a problem as to how can i destroy the shared memory segments i've created. A simplified version of my code is as follows | | \ / v [CODE] int main() { //connection to clients an stuff prossid = fork() if(prossid == 0) { //do stuff for child prossess } else { close(ns)//if it's the father prossess close the connection with the client program } }[/CODE] …

Member Avatar
Member Avatar
+0 forum 3

If i fork off a parent and want to fork of fthe child, can i pass new variables from the child to the "grand -child"? PL help me clear this concept.

Member Avatar
+0 forum 0

I wasnt to fork from a parent and execute two perl codes simultaneously rot13.pl and pi.pl.I want to keep track of the pid's of both children. i am not aware how to do it within the system() cmd. I have written the code below. But something tells me I'm being repetetive and round about. Any help? [code=perl] #!usr/bin/perl5 #child processes running trial sub run_rot13{ ($x,$y)=@_; system(" perl $x.pl <$y &"); print("\n$$\n"); } sub run_pi{ ($x,$y)=@_; system("perl $x.pl <$y &"); print("\n$$\n"); } unless($pid=fork) { run_rot13(rot13,lista); exit; } print("$$\n$pid\n"); unless($pid=fork) { run_pi(pi,no_trials); exit; } print("$$\n$pid\n"); [/code]

Member Avatar
+0 forum 0

hi, i have problems using exec(), I need to pass a set of numbers using exec to another program that is supposed to use them to calculate something,,, i dont know how to pass them any suggestions??

Member Avatar
Member Avatar
+0 forum 1

Could someone please advise me on how to implement fork() in a parallel processing (multiplying) of a 2d array....I have been at this for far too long.. here is my code [code] for (int i = 0; i < rowCol[0]; i++ ) result[i] = (float*)malloc ( rowCol[1] * sizeof(float) ); int i = 0; int j = 0; int k = 0; for(i = 0; i < rowCol[0]; i++) { for( j = 0; j <rowCol[3]; j++) { result[i][j] = 0; for( k = 0; k < rowCol[1]; k++) { result[i][j] += (matrix1[i][k] * matrix2[k][j]); } } } [/code] Any …

Member Avatar
Member Avatar
+0 forum 8

I want to launch a new process from a given string of input (including parameter). I tried to use system() but it appears that in the child process, getppid() is not the same as the caller in some runs (probably because system uses the shell as command interpreter). However, if I fork and then use exec(), it is complicated to parse the parameter. Anyone knows a way to get around this problem? Or is there any library that helps parse a string of input into program parameters like shells often do? Thank you very much in advance.

Member Avatar
Member Avatar
+0 forum 3

Hi all, I'm helping my girl out with her hw, (/me not knowing C at all, but someone has to help, right?) I am supposed to read user input, and then try to execute a given command plus the arguments in every given $PATH. I managed it all, but I get puzzled by the execv function, which I am supposed to be using, it doesn't produce any stdio, and I can't seem to find a way to make it work a basic command [CODE]execv ("/bin/touch", "/tmp/test123", NULL);[/CODE] doesn't do anything, neither do I see an exit code. How do I …

Member Avatar
Member Avatar
+1 forum 6

Hi, I was honing my linux programming skill when this nuisance started bugging me. I wanted to create an empty file creator program. While creating a large file it must print # for progress bar. But the output shows it happening reverse way. ie. first it copies file and shows the progress bar(although the bar is filled completely thus showing that parent process is working correctly). Kindly help me in putting these progress # simultaneously to file copying. Thanks in advance. Code: [CODE] #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> static int status …

Member Avatar
Member Avatar
+0 forum 3

The End.