10 Replies - 8750 Views - Last Post: 25 April 2009 - 03:03 AM Rate Topic: -----

#1 code_love  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-July 07

semaphore...Resource Temporary unavailable

Posted 21 April 2009 - 03:59 PM

Hi all,
I keep getting error message that "Resource Temporary unavailable".. Here is the code. if someone point out where the problem is, it will be great

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#define BUFSZ 51200

int main()
{
	int pid; //store the pid of the proccess.
	int semid; //store the semaphore id
	int nsems =  1; //number of semaphores
	int flags=0666; 
	struct sembuf buf1; //used for semop system call.
	char *msg;
	int shmid; //shared memory id.
	int msgid; 
	
	// creating the semaphore
	semid = semget(IPC_PRIVATE,nsems,flags);
	if (semid < 0)
	{
		perror("semget");
		exit(EXIT_FAILURE);
	}
	
	//printing semaphore id
	printf("semid is: %d \n", semid);

	//inilizting the semaphore to 1
	typedef union semun
	{
		int val;
		struct semid_ds *buf;
		ushort * array;
	}semun_t;
	semun_t arg;
	arg.val = 0;
	
	if(semctl(semid,0,SETVAL,arg)<0)
	{
		perror("semctl");
		exit(EXIT_FAILURE);
	}

	buf1.sem_num=0;
	buf1.sem_flg=IPC_NOWAIT;

	
	//creating shared memory
	shmid = shmget(IPC_PRIVATE,BUFSZ,0666);
	if (shmid<0)
	{
		perror ("shmget");
		exit(EXIT_FAILURE);
	}
	msg = shmat(shmid,0,SHM_RND);

	//creating message queue.
	msgid = msgget(IPC_PRIVATE, 0666);

	pid = fork();

	if (pid<0)
	{
		printf("error %d",pid);
	}
	else if (pid == 0)
	{	
		

		buf1.sem_op=-1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		}
		
		msgrcv(msgid,msg,BUFSZ,0,0);
		
		buf1.sem_op=1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		}
				
		printf("Found the string in shared memory %s ",msg);
		
		printf("\nThere are 1 processes attached to shared memory region %d ", shmid);
		printf("\n");
				

	}
	else
	{
		buf1.sem_op=-1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		} 		
				
		sprintf( msg, "%s %d >", "<The Prosscess ID is ", pid );
		msgsnd(msgid, msg, BUFSZ, 0);		
		printf("\nParent placed the string %s\n", msg);
		buf1.sem_op=1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		} 
		
	}	

	 
}


This post has been edited by code_love: 21 April 2009 - 04:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: semaphore...Resource Temporary unavailable

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: semaphore...Resource Temporary unavailable

Posted 22 April 2009 - 04:07 AM

You don't appear to have included the <sys/msg.h> library.
Read more here:
http://www.cs.cf.ac....e/C/node25.html
Was This Post Helpful? 0
  • +
  • -

#3 code_love  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-July 07

Re: semaphore...Resource Temporary unavailable

Posted 22 April 2009 - 08:55 AM

Hi,
thanks for pointing this out. Actually I missed it, but it's not the problem. even when add #include<sys/msg.h>, I still get error when child tries to "wait" the semaphore, the error Resource Temporary Unavailable.
The thing, I just want the parent code to execute before the child, so I used semaphore to do the it. but what am I doing wrong :( ? .
like
parent_code;
signal();
====
wait()
child code
signal()



just one more thing, I removed:
		buf1.sem_op=-1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		}  


from the parent code....

This post has been edited by code_love: 22 April 2009 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: semaphore...Resource Temporary unavailable

Posted 22 April 2009 - 02:26 PM

What does your new code look like now?

Just so we are all working from the same code.
Was This Post Helpful? 0
  • +
  • -

#5 code_love  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-July 07

Re: semaphore...Resource Temporary unavailable

Posted 23 April 2009 - 03:01 PM

thanks for your replay. Here is the code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFSZ 4096

int main()
{
	int pid; //store the pid of the proccess.
	int semid; //store the semaphore id
	int nsems =  1; //number of semaphores
	int flags=0666; 
	struct sembuf buf1; //used for semop system call.
	char *msg;
	int shmid; //shared memory id.
	int msgid; 
	
	// creating the semaphore
	semid = semget(IPC_PRIVATE,nsems,flags);
	if (semid < 0)
	{
		perror("semget");
		exit(EXIT_FAILURE);
	}
	
	//printing semaphore id
	printf("semid is: %d \n", semid);

	//inilizting the semaphore to 1
	typedef union semun
	{
		int val;
		struct semid_ds *buf;
		ushort * array;
	}semun_t;
	semun_t arg;
	arg.val = 0;
	
	if(semctl(semid,0,SETVAL,arg)<0)
	{
		perror("semctl");
		exit(EXIT_FAILURE);
	}

	buf1.sem_num=0;
	buf1.sem_flg=IPC_NOWAIT;

	
	//creating shared memory
	shmid = shmget(IPC_PRIVATE,BUFSZ,0666);
	if (shmid<0)
	{
		perror ("shmget");
		exit(EXIT_FAILURE);
	}
	msg = shmat(shmid,0,SHM_RND);

	//creating message queue.
	msgid = msgget(IPC_PRIVATE, 0666);

	pid = fork();

	if (pid<0)
	{
		printf("error %d",pid);
	}
	else if (pid == 0)
	{
		buf1.sem_op=-1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		}
	
		msgrcv(msgid,msg,BUFSZ,0,0);
				
		printf("Found the string in shared memory %s ",msg);
		
		buf1.sem_op=+1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		}

		
		printf("\nThere are 1 processes attached to shared memory region %d ", shmid);
		printf("\n");
				

	}
	else
	{	
		sprintf( msg, "%s %d >", "<The Prosscess ID is ", pid );
		msgsnd(msgid, msg, BUFSZ, 0);		
		printf("\nParent placed the string %s\n", msg);		
		
		buf1.sem_op=+1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			perror("semop");
		}
	}	

	 
}


The output:
semop: Resource temporarily unavailable
Found the string in shared memory <The Prosscess ID is  5078 > 
There are 1 processes attached to shared memory region 655379 

Parent placed the string <The Prosscess ID is  5078 >




So I just want the parent to execute before the child, but i don't really what I did wrong or why I'm getting the error.

This post has been edited by code_love: 23 April 2009 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: semaphore...Resource Temporary unavailable

Posted 23 April 2009 - 07:06 PM

Could it be because of this buf1.sem_flg=IPC_NOWAIT;
Was This Post Helpful? 0
  • +
  • -

#7 code_love  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-July 07

Re: semaphore...Resource Temporary unavailable

Posted 23 April 2009 - 11:44 PM

View Postskaoth, on 23 Apr, 2009 - 06:06 PM, said:

Could it be because of this buf1.sem_flg=IPC_NOWAIT;

then how can I fix it?
Note: I'm using kubuntu on VBox.
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: semaphore...Resource Temporary unavailable

Posted 24 April 2009 - 05:18 AM

View Postcode_love, on 23 Apr, 2009 - 10:44 PM, said:

View Postskaoth, on 23 Apr, 2009 - 06:06 PM, said:

Could it be because of this buf1.sem_flg=IPC_NOWAIT;

then how can I fix it?
Note: I'm using kubuntu on VBox.


And what is the compiler?
I would have expected "gcc'.
Is that right?

Using "gcc" I get no error.

Humour me and try replacing
perror("semop");
with
strerror(semop(semid,&buf1,nsems));

I suspect no change but straws are being grasped at here.
Was This Post Helpful? 1
  • +
  • -

#9 code_love  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-July 07

Re: semaphore...Resource Temporary unavailable

Posted 24 April 2009 - 10:56 AM

ahhh, the error gone :o and yes it's gcc.
But I still have problem: parent code executes after the child's code.
Here is the updated code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFSZ 4096

int main()
{
	int pid; //store the pid of the proccess.
	int semid; //store the semaphore id
	int nsems =  1; //number of semaphores
	int flags=0666; 
	struct sembuf buf1; //used for semop system call.
	char *msg;
	int shmid; //shared memory id.
	int msgid; 
	
	// creating the semaphore
	semid = semget(IPC_PRIVATE,nsems,flags);
	if (semid < 0)
	{
		perror("semget");
		exit(EXIT_FAILURE);
	}
	
	//printing semaphore id
	printf("semid is: %d \n", semid);

	//inilizting the semaphore to 1
	typedef union semun
	{
		int val;
		struct semid_ds *buf;
		ushort * array;
	}semun_t;
	semun_t arg;
	arg.val = 0;
	
	if(semctl(semid,0,SETVAL,arg)<0)
	{
		perror("semctl");
		exit(EXIT_FAILURE);
	}

	buf1.sem_num=0;
	buf1.sem_flg=IPC_NOWAIT;

	
	//creating shared memory
	shmid = shmget(IPC_PRIVATE,BUFSZ,0666);
	if (shmid<0)
	{
		perror ("shmget");
		exit(EXIT_FAILURE);
	}
	msg = shmat(shmid,0,SHM_RND);

	//creating message queue.
	msgid = msgget(IPC_PRIVATE, 0666);

	pid = fork();

	if (pid<0)
	{
		printf("error %d",pid);
	}
	else if (pid == 0)
	{
		buf1.sem_op=-1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			strerror(semop(semid,&buf1,nsems));
		}
	
		msgrcv(msgid,msg,BUFSZ,0,0);
				
		printf("Found the string in shared memory %s ",msg);
		
		buf1.sem_op=1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			strerror(semop(semid,&buf1,nsems));
		}

		
		printf("\nThere are 1 processes attached to shared memory region %d ", shmid);
		printf("\n");
				

	}
	else
	{	
		sprintf( msg, "%s %d >", "<The Prosscess ID is ", pid );
		msgsnd(msgid, msg, BUFSZ, 0);		
		printf("\nParent placed the string %s\n", msg);		
		
		buf1.sem_op=1;
		if ((semop(semid,&buf1,nsems))<0)
		{
			strerror(semop(semid,&buf1,nsems));;
		}
	}	

	 
}



and the output:
semid is: 196614 
Found the string in shared memory <The Prosscess ID is  5962 > 
There are 1 processes attached to shared memory region 884762 

Parent placed the string <The Prosscess ID is  5962 >


Actually I want it to be:
semid is: 196614 
Parent placed the string <The Prosscess ID is  5962 >
Found the string in shared memory <The Prosscess ID is  5962 > 
There are 1 processes attached to shared memory region 884762 


I still don't understand why?? I mean I used wait() at child section, why it doesn't wait!??
Was This Post Helpful? 0
  • +
  • -

#10 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: semaphore...Resource Temporary unavailable

Posted 24 April 2009 - 05:24 PM

I've already answered this problem. please re-read the documentation on semop.

You need to remove the IPC_NOWAIT flag from the semaphore. Just delete that line I showed earlier. This flag indicates to the semaphore operation not to wait for the semaphore to be signaled.

edit:
I went back and found that I had to set sem_flg = 0;
A quick way to make sure that this is working correctly is to put the parent to sleep() before
incrementing/signaling the semaphore. Here is what I have.

...
buf1.sem_num=0;
buf1.sem_flg= 0; //IPC_NOWAIT;

f (pid<0)
	{
			printf("error %d",pid);
		}
	else if (pid == 0)
	{
			printf("Child acquiring semaphore\n");
			buf1.sem_op=-1;
			if ((semop(semid,&buf1,nsems))<0)
			{
				strerror(semop(semid,&buf1,nsems));
			}

			msgrcv(msgid,msg,BUFSZ,0,0);

			printf("Child: Found the string in shared memory %s ",msg);

			buf1.sem_op=1;
			if ((semop(semid,&buf1,nsems))<0)
			{
						strerror(semop(semid,&buf1,nsems));
			 }


			printf("\nChild: There are 1 processes attached to shared memory region %d\n", shmid);

		}
	else
	{
			sprintf( msg, "Parent <%s %d >", "<The Prosscess ID is ", pid );
			msgsnd(msgid, msg, BUFSZ, 0);
			printf("\nParent placed the string %s\n", msg);

			printf("Parent waiting before locking semaphore\n");
			sleep(5);
			buf1.sem_op=1;
			if ((semop(semid,&buf1,nsems))<0)
			{
					strerror(semop(semid,&buf1,nsems));;
			}
	}



Output:
[email protected]:~/code$ ./main
semid is: 1638448
Child acquiring semaphore

Parent placed the string Parent <<The Prosscess ID is 23620 >
Parent waiting before locking semaphore
Child: Found the string in shared memory Parent <<The Prosscess ID is 23620 >
Child: There are 1 processes attached to shared memory region 34701415

This post has been edited by skaoth: 24 April 2009 - 06:39 PM

Was This Post Helpful? 1
  • +
  • -

#11 code_love  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-July 07

Re: semaphore...Resource Temporary unavailable

Posted 25 April 2009 - 03:03 AM

exactly, you are right!
thank you so much skaoth and also thank you so much janotte. really your help is appreciated. thanks again.

This post has been edited by code_love: 25 April 2009 - 03:04 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1