LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2009, 10:24 AM   #1
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Rep: Reputation: 15
Need some help in modifying a program.


Respected sir,

I have a producer-consumer programs that uses two semaphores to to control the shared resoures. I want to modify these two programs in such a way that it uses only single semaphore. I need to use an integer instead of array in Union.

Here are the programs:

=================
PRODUCER
=================

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>

#include <sys/sem.h>
#define PUB7645 "./PubFile"

union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
int
main() {
int i=0, intValue, semID;
key_t key;
FILE *fptr;

union semun arg;
static ushort startVal[2] = {1, 0};
static struct sembuf acquire = {0, -1, SEM_UNDO},
release = {1, 1, SEM_UNDO};
key = ftok(".", 's');

if ((semID = semget(key, 2, IPC_CREAT | 0666)) == -1) {
perror("Sem Creation:");
exit(2);
}

arg.array = startVal;
if (semctl(semID, 2, SETALL, arg) == -1 ) {
perror("semctl: SETALL");
exit(3);
}
for( ; ; ) {
srand(getpid()+i);
intValue =rand()*49/RAND_MAX + 1;

if (semop(semID, &acquire, 1) == -1) {
perror("Producer waiting");
exit(4);
}

if ((fptr = fopen(PUB7645, "w")) == NULL) {
perror(PUB7645);
exit(1);
}
fprintf(fptr, "%d\n", intValue);
fclose(fptr);

if (semop(semID, &release, 1) == -1) {
perror("new");
exit(5);
}
i++;
}
}
=================
Consumer:
=================

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>

#include <sys/sem.h>
#define PUB7645 "./PubFile"

union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
int main() {
int i=0, intValue, semID;
key_t key;
FILE *fptr;

union semun arg;
static struct sembuf acquire = {1, -1, SEM_UNDO},
release = {0, 1, SEM_UNDO};

key = ftok(".", 's');

if ((semID = semget(key, 2, IPC_CREAT | 0666)) == -1) {
perror("Sem Creation:");
exit(2);
}
for(i=1; i<=5 ; i++) {

if (semop(semID, &acquire, 1) == -1) {
perror(“Consumer waiting");
exit(4);
}

if ((fptr = fopen(PUB7645, "r")) == NULL) {
perror(PUB7645);
exit(1);
}

fscanf(fptr, "%d", &intValue);
fclose(fptr);
if (semop(semID, &release, 1) == -1) {
perror(“Release");
exit(4);
}

printf("%d\t", intValue);
}
printf("\nI am process %d. I got five numbers ", getpid());
printf("from a producer as above.\n");
}

Thanks.
 
Old 04-28-2009, 02:52 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It looks like a homework question, so I intend to give general guidelines. However, in this case the solution seems obvious to me, as the semaphores are always used together. It should be really simple to change. If you have a specific problem with some details/functions, write about it.
 
Old 04-28-2009, 03:59 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I'm curious. Are you at U Regina? Or does your instructor just use material from there?

Either way, try reading the very clear explanation of your current topic at:

http://www.cs.uregina.ca/Links/class...semaphore.html
 
Old 04-28-2009, 04:49 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
@LinuxInfo: at this point you have managed to post 4 assignment questions:
http://www.linuxquestions.org/questi...ogram.-704904/
http://www.linuxquestions.org/questi...m.....-707332/
http://www.linuxquestions.org/questi...ogram.-717216/
and this thread.

As johnsfine and others have indicated posting homework is not appreciated if you don't put in any effort yourself.
You may consider this a friendly nudge but beware of the consequences if you don't care and persist in your ways.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Modifying a Linux program?? Jim Isbell Linux - Software 11 03-17-2009 05:20 PM
modifying the installer soybean pete Slackware - Installation 2 06-15-2005 04:30 PM
Modifying Desktop ElementNine Linux - General 1 10-09-2003 04:20 AM
Modifying afterstep naima Programming 0 11-02-2001 08:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration