Sponsored Content
Full Discussion: synchronize two processes
Top Forums Programming synchronize two processes Post 302207891 by emil2006 on Saturday 21st of June 2008 12:54:37 PM
Old 06-21-2008
synchronize two processes

i am trying to synchronize between father process and son process
created by fork() command, to print simultaneously.

my program is written in c under bash shell.
the compile goes ok but when i try to run nothing happens and the program doesnot end.

my code is:

Code:
 #include <stdio.h>
#include <semaphore.h>
#include <stdlib.h>
main() {
  sem_t s1;
  sem_t s2;
  int value1 , value2;

  sem_init(&s1, 0, 1);
  sem_init(&s2,0,1);
 
  sem_wait(&s2);
  
  
  int pid = fork();
  
  
  if (pid != 0)
  	{
  		int i;
  		for (i=0 ; i<=10 ; i++)
  		{
  			sem_getvalue (&s1,&value1);
  			while (value1 != 1)
  			  sem_getvalue (&s1,&value1);
  			  
  			sem_wait(&s1);
  			if (i%2 != 0)
  				printf("%d\n",i);
  				
  			sem_post(&s2);
  		}
  	}
  	
  	
  	if (pid == 0)
  	{
  		int j;
  		for (j=0 ; j<=10 ; j++)
  		{
  			sem_getvalue (&s2,&value2);
  			while (value2 != 1)
  			  sem_getvalue (&s2,&value2);
  			  
  			sem_wait(&s2);
  			if (j%2 == 0)
  				printf("%d\n",j);
  				
  			sem_post(&s1);	  
  		}
  		exit(0);
  	}
  
  
  
 return 0;
}

is there any other way to synchronize father and son process ?

Last edited by emil2006; 06-22-2008 at 05:34 AM..
 

9 More Discussions You Might Find Interesting

1. Programming

synchronize as in java

Hi, I am trying to implement the synchronize feature of java using C. I am using a semaphore for the same. I have a wrapper called "synch" to which I pass the function pointer(any_fn). This pointer points to the function (my_fn) which needs to be synchronized. However to create the semaphore I... (8 Replies)
Discussion started by: linuxpenguin
8 Replies

2. Shell Programming and Scripting

How to synchronize all the linux machine?

Hi, I have three Linux machine have three different times. Can I synchronize them using one process? I have root access. Thanks! (1 Reply)
Discussion started by: whatisthis
1 Replies

3. Shell Programming and Scripting

how to synchronize different dirs

I have 4 directory Dir1 file1 file2 file3 file4 Dir2 file3 file5 file6 file8 Dir3 file1 file2 file6 file9 file10 Dir4 file3 file6 file12 file15 and all the 4 dirs are having couple of files. Few of the files are common to other directory/ies and few... (1 Reply)
Discussion started by: reldb
1 Replies

4. Programming

need a way to synchronize processes

I am writing a program in C for my networking class, so I am relatively new to this. To begin, I have 7 processes that need do send messages to every other one, and every one of them needs to receive the messages sent by others. I am using fork() to create 6 more processes. The message... (1 Reply)
Discussion started by: inabramova
1 Replies

5. UNIX for Advanced & Expert Users

synchronize processes

hi. i am writing a c program under bash shell. i would like to use semaphore functions like sem_wait(), sem_post() and i included <semaphore.h> and it compailes fine but when i try to run it gives me an error "undefined reference to sem_wait() , sem_post() , sem_init()" what have i missed... (2 Replies)
Discussion started by: emil2006
2 Replies

6. Shell Programming and Scripting

Synchronize Files-Help

Hi, I have two servers1&2, one is not in the network. Cant communicate from it to other servers. The second one can communicate to above mentioned server. I am trying a script which synchronizes files between server 1 an 2? server1: cant communicate to any other servers server2: can... (4 Replies)
Discussion started by: Tuxidow
4 Replies

7. Shell Programming and Scripting

How to synchronize using FTP

Hi everyone! I need to write a script that will synchronize two servers using FTP. So basically the script will get only the files that exist on the remote server that do not exist on the local server. Is there an option to do this when using mget? If not, is there a way to copy over only the... (2 Replies)
Discussion started by: Fatbob
2 Replies

8. AIX

Verify and Synchronize HACMP

Hi Earlier we used to move the resource groups between nodes manually. Recently I have added the HACMP startup and stop scripts. Then I moved the resource group to see if everything works fine as per the startup and stop scripts, but it didn't work out as expected. Do i need to do bring... (4 Replies)
Discussion started by: samsungsamsung
4 Replies

9. UNIX for Advanced & Expert Users

How to synchronize two different files?

Hello, I have the latest stable release of a UNIX-like O.S. in the ISO format, and want to synchronize it with the latest Release Candidate (RC) of it, in order to reducing bandwidth usage. The ISO images for that O.S. is provided via different protocols including rsync, FTP and HTTP Is this... (4 Replies)
Discussion started by: temp-usr
4 Replies
sem_init(3RT)						    Realtime Library Functions						     sem_init(3RT)

NAME
sem_init - initialize an unnamed semaphore SYNOPSIS
cc [ flag... ] file... -lrt [ library... ] #include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); DESCRIPTION
The sem_init() function is used to initialize the unnamed semaphore referred to by sem. The value of the initialized semaphore is value. Following a successful call to sem_init(), the semaphore may be used in subsequent calls to sem_wait(3RT), sem_trywait(3RT), sem_post(3RT), and sem_destroy(3RT). This semaphore remains usable until the semaphore is destroyed. If the pshared argument has a non-zero value, then the semaphore is shared between processes; in this case, any process that can access the semaphore sem can use sem for performing sem_wait(3RT), sem_trywait(3RT), sem_post(3RT), and sem_destroy(3RT) operations. Only sem itself may be used for performing synchronization. The result of referring to copies of sem in calls to sem_wait(3RT), sem_try- wait(3RT), sem_post(3RT), and sem_destroy(3RT), is undefined. If the pshared argument is zero, then the semaphore is shared between threads of the process; any thread in this process can use sem for performing sem_wait(3RT), sem_trywait(3RT), sem_post(3RT), and sem_destroy(3RT) operations. The use of the semaphore by threads other than those created in the same process is undefined. Attempting to initialize an already initialized semaphore results in undefined behavior. RETURN VALUES
Upon successful completion, the function initializes the semaphore in sem. Otherwise, it returns -1 and sets errno to indicate the error. ERRORS
The sem_init() function will fail if: EINVAL The value argument exceeds SEM_VALUE_MAX. ENOSPC A resource required to initialize the semaphore has been exhausted, or the resources have reached the limit on semaphores (SEM_NSEMS_MAX). ENOSYS The sem_init() function is not supported by the system. EPERM The process lacks the appropriate privileges to initialize the semaphore. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sem_destroy(3RT), sem_post(3RT), sem_wait(3RT), attributes(5), standards(5) SunOS 5.10 28 Jun 2002 sem_init(3RT)
All times are GMT -4. The time now is 09:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy