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
SD_JOURNAL_STREAM_FD(3) 				       sd_journal_stream_fd					   SD_JOURNAL_STREAM_FD(3)

NAME
sd_journal_stream_fd - Create log stream file descriptor to the journal SYNOPSIS
#include <systemd/sd-journal.h> int sd_journal_stream_fd(const char* identifier, int priority, int level_prefix); DESCRIPTION
sd_journal_stream_fd() may be used to create a log stream file descriptor. Log messages written to this file descriptor as simple newline-separated text strings are written to the journal. This file descriptor can be used internally by applications or be made STDOUT/STDERR of other processes executed. sd_journal_stream_fd() takes a short program identifier string as first argument, which will be written to the journal as _SYSLOG_IDENTIFIER= field for each log entry (see systemd.journal-fields(7) for more information). The second argument shall be the default priority level for all messages. The priority level is one of LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG, as defined in syslog.h, see syslog(3) for details. The third argument is a boolean: if true kernel-style log priority level prefixes (such as SD_WARNING) are interpreted, see sd-daemon(3) for more information. It is recommended that applications log UTF-8 messages only with this API, but this is not enforced. RETURN VALUE
The call returns a valid write-only file descriptor on success or a negative errno-style error code. NOTES
The sd_journal_stream_fd() interface is available as a shared library, which can be compiled and linked to with the libsystemd-journal pkg- config(1) file. EXAMPLES
Creating a log stream suitable for fprintf(3): #include <syslog.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <systemd/sd-journal.h> #include <systemd/sd-daemon.h> int main(int argc, char *argv[]) { int fd; FILE *log; fd = sd_journal_stream_fd("test", LOG_INFO, 1); if (fd < 0) { fprintf(stderr, "Failed to create stream fd: %s ", strerror(-fd)); return 1; } log = fdopen(fd, "w"); if (!log) { fprintf(stderr, "Failed to create file object: %m "); close(fd); return 1; } fprintf(log, "Hello World! "); fprintf(log, SD_WARNING "This is a warning! "); fclose(log); return 0; } SEE ALSO
systemd(1), sd-journal(3), sd-daemon(3), sd_journal_print(3), syslog(3), fprintf(3), systemd.journal-fields(7) systemd 208 SD_JOURNAL_STREAM_FD(3)
All times are GMT -4. The time now is 06:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy