pipe() - Sincronization

Thread Tools Search this Thread
Top Forums Programming pipe() - Sincronization
# 1  
Old 08-16-2011
pipe() - Sincronization

Hi guys.
I've a problem with pipes, I'm trying to make a program that can create a child process and they must generate the folowing output:
Ping ... Pong
Ping ... Pong
Ping ... Pong

I want syncronize the output whithout using the semephores, can anyone help me?
Thanks in advance.

* @file main.c
* @brief Description
* @date 01-01-2006
* @author eiYxxxxx@student.estg.ipleiria.pt

#define FFORK 1
#define FPIPE 2
#define FIO 3
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>

void error_ (int code, char *message, ...){
fprintf(stderr,"%s \n", message);

int main(int argc, char *argv[])
/* Variable declaration */
int fd_p[2];
int fd_s[2];
int buf = 0;
int WRITE = 1;
int NO_WRITE = 0;
ssize_t io_bytes = 0;
static int j = 0;

/* Main Code*/
(void) argc, (void) argv;
if ((pipe(fd_s) != 0) || (pipe(fd_p) != 0))
error_(FPIPE,"Pipe Failed!\n");

setvbuf(stdout,(char*) NULL,_IONBF,0);

switch (fork()){
case -1:
error_(FFORK,"Failed Fork!\n");
case 0 :

while (1){

if (j == 0){
if (close(fd_p[1]) != 0)
error_(FIO,"Failed Close!\n");
if (close(fd_s[0]) != 0)
error_(FIO,"Failed Close!\n");
if ((io_bytes = write(fd_s[1],&NO_WRITE, sizeof(int))) == -1)
error_(FIO,"Failed Write!\n");

if ((io_bytes = read (fd_p[0], &buf, sizeof(int))) == -1)
error_(FIO,"Failed Read");

if (buf == WRITE){
if ((io_bytes = write(fd_s[1],&WRITE, sizeof(int))) == -1)
error_(FIO,"Failed Write!\n");


while (1){

if (j==0){
if (close(fd_p[0]) != 0)
error_(FIO,"Failed Close!\n");
if (close(fd_s[1]) != 0)
error_(FIO,"Failed Close!\n");

if ((io_bytes = write(fd_p[1],&NO_WRITE,sizeof(int))) == -1)
error_(FIO,"Failed Write!\n");
/* Starting the Synchrinization */
if (j==0)
if ((io_bytes = write(fd_p[1], &WRITE, sizeof(int))) == -1)
error_(FIO,"Write Failed!\n");
buf = NO_WRITE;
if ((io_bytes = read(fd_s[0], &buf, sizeof(int))) == -1)
error_(FIO,"Failed Read!\n");
if (buf==WRITE){
printf("Ping... ");
if ((io_bytes = write(fd_p[1],&WRITE,sizeof(int))) == -1)
error_(FIO,"Failed Write");


return 0;
# 2  
Old 08-16-2011
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

2. Red Hat

Broken pipe

Hi all, I am trying to run a sleep command and I am receiving a broken pipe when I execute. any help would be greatly appreciated. sleep `< /dev/urandom tr -dc 0-6 | head -c2` (2 Replies)
Discussion started by: gartie
2 Replies

3. UNIX for Dummies Questions & Answers

pipe > output

I can use pipe output to a file. For example ./somescript.sh > output.txt But for example if the output from ./somescript.sh is slow. like if it prints one line every minute then output.txt is not updated every minute. Lines are written to output.txt in one go, hence have to wait for the whole... (2 Replies)
Discussion started by: kevincobain2000
2 Replies

4. UNIX for Dummies Questions & Answers

xargs vs. pipe

I have been using unix on and off for a number of years. I am not a sys admin. I use what I need. I have googled this, but I really can't figure out what is the difference between using xarg and just using a regular pipe? Why do I need to include xarg sometimes and how do I know when I need it? (2 Replies)
Discussion started by: guessingo
2 Replies

5. Shell Programming and Scripting

pipe in command

Hello, I try to concatenate a command to execute. Sadly it throws an error. #!/bin/bash cd / cmd="find -name *.txt | awk '{ printf "FILE: "$1; system("less "$1);}' | egrep 'FILE:|$1'" echo "1." $($cmd) echo "2." $("$cmd") echo "3." `$cmd` echo "4." `"$cmd"`1.&3. 'find: paths must... (2 Replies)
Discussion started by: daWonderer
2 Replies

6. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

7. Shell Programming and Scripting

How can I use pipe

Hi, guys: I am working on my shell using c. How can I use pipe to implement the following? ls -l 1>> | grep hellp 1<< 2>> | less 2<< (the output of ls goes to grep, and the output of grep goes to less) Thanks Please use and tags when posting code, data or logs etc. to preserve... (1 Reply)
Discussion started by: tomlee
1 Replies

8. Programming

unix pipe in C

I currently stuck on a simple program that requires unix pipe. I'm have never programmed with unix pipe before, so if anyone can point me to the right different will be greatly appreciated! I'm suppose to write a program that the parent spawns many child processes and each of the child process... (1 Reply)
Discussion started by: meh
1 Replies

9. UNIX for Dummies Questions & Answers

sincronization clock unix with windows server..

hi all... how i can sincronization clock solaris server with windows server....both servers i connected in the same network... thank you.... Best Regards... (4 Replies)
Discussion started by: chanfle
4 Replies

10. Programming

pipe help

i made a lot of processes. here is the code: main() { printf("\nEnter K="); scanf("%d",&k); printf("Enter L="); scanf("%d",&l); printf("\nFather id=%d\n",getpid()); x=0; makechild(); sleep(2); return 1; } int makechild() { for(q=1;q<=k;q++) { if(f=fork()) { ... (5 Replies)
Discussion started by: bb666
5 Replies
Login or Register to Ask a Question