Sponsored Content
Full Discussion: pipe() - Sincronization
Top Forums Programming pipe() - Sincronization Post 302547768 by pharaoh on Tuesday 16th of August 2011 02:05:55 PM
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);
exit(code);
}


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");
break;
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){
printf("Pong!\n");
if ((io_bytes = write(fd_s[1],&WRITE, sizeof(int))) == -1)
error_(FIO,"Failed Write!\n");
}
sleep(1);
j++;
}
break;

default:

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)
printf("Ping...");
if ((io_bytes = write(fd_p[1], &WRITE, sizeof(int))) == -1)
error_(FIO,"Write Failed!\n");
else
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");
}
sleep(1);
j++;
}

}

return 0;
}
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PIPE(2) 						     Linux Programmer's Manual							   PIPE(2)

NAME
pipe, pipe2 - create pipe SYNOPSIS
#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <fcntl.h> /* Obtain O_* constant definitions */ #include <unistd.h> int pipe2(int pipefd[2], int flags); DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For fur- ther details, see pipe(7). If flags is 0, then pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior: O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open file descriptions. Using this flag saves extra calls to fcntl(2) to achieve the same result. O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors. See the description of the same flag in open(2) for reasons why this may be useful. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE Too many file descriptors are in use by the process. ENFILE The system limit on the total number of open files has been reached. VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9. CONFORMING TO
pipe(): POSIX.1-2001. pipe2() is Linux-specific. EXAMPLE
The following program creates a pipe, and then fork(2)s to create a child process; the child inherits a duplicate set of file descriptors that refer to the same pipe. After the fork(2), each process closes the descriptors that it doesn't need for the pipe (see pipe(7)). The parent then writes the string contained in the program's command-line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, " ", 1); close(pipefd[0]); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } SEE ALSO
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2012-02-14 PIPE(2)
All times are GMT -4. The time now is 05:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy