473,466 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reader writter problem

I just implemented a program to solve reader writter prob. Wenever I
run the program it seems to go into S+ state and it waits there I dont
know why. When i attach gdb to one of the childs and step through for
some lines and dettach it runs for some time again ?????

The program is listed below. The listing is kind of
long

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>

extern int errno;
static pid_t *child;
static int count;

enum { reader , writter };

union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};

void handle( int signo )
{
for( int i = 0 ; i < count ; i++ )
kill( child[i] , SIGINT );
}

void acquire_lock( int sem_id , int s_num )
{
struct sembuf s[2];

s[0].sem_num = s[1].sem_num = s_num;
s[0].sem_flg = s[1].sem_flg = 0;
s[0].sem_op = 0;
s[1].sem_op = 1;

semop( sem_id , s , 2 );
}

void release_lock( int sem_id , int s_num )
{
union semun s;

s.val = 0;

semctl( sem_id , s_num , SETVAL , s );
}

void reader_writer( int sem_id , int shm_id )
{
while( 1 ) {

if( rand() % 2 ) {
/* becomes a writter */
/* acquire a lock of the resource */
acquire_lock( sem_id , writter );

printf( "Writing a file:%d\n" , getpid() );
sleep( rand() % 10 );
printf( "Finished writting file:%d\n" , getpid() );

/* release lock */
release_lock( sem_id , writter );
}
else {
/* become a reader */
int *rcount = (int*) shmat( shm_id , NULL /* choose any place to
attach */ , 0 );

/* acquire lock for count manip */
printf( "Acquiring read lock:%d\n" , getpid() );
acquire_lock( sem_id , reader );
printf( "Acquiring write lock:%d\n" , getpid() );
acquire_lock( sem_id , writter );

printf( "%d\n" , *rcount );
(*rcount)++;
printf( "%d\n" , *rcount );

/*release the lock for count */
printf( "Releasing read lock:%d\n" , getpid() );
release_lock( sem_id , reader );

printf( "Doing some reading on the file:%d\n" , getpid() );
sleep( rand() % 10 );
printf( "Finished reading file:%d\n" , getpid() );

/* acquire the lock for count */
printf( "Acquiring read lock:%d\n" , getpid() );
acquire_lock( sem_id , reader );

(*rcount)--;
printf( "%d\n" , *rcount );
if( !(*rcount) ) {
/* last reader process so release the writter lock */
release_lock( sem_id , writter );
}

/* release the lock on count */
printf( "Releasing read lock:%d\n" , getpid() );
release_lock( sem_id , reader );
shmdt( rcount );
sleep( 2 );
}
}
}

int main( int argc , char** argv )
{
int sem_id;
int shm_id;

count = atoi( argv[2] );
child = (pid_t*) malloc( sizeof( pid_t ) * count );

if( ( sem_id = semget( atoi( argv[1] ) , 2 , IPC_CREAT | S_IRWXU ) )
== -1 ) {
fprintf( stderr , "%s\n" , strerror( errno ) );
exit( EXIT_FAILURE );
}

if( ( shm_id = shmget( atoi( argv[1] ) , sizeof( int ) , IPC_CREAT |
S_IRWXU ) ) == -1 ) {
fprintf( stderr , "%s\n" , strerror( errno ) );
exit( EXIT_FAILURE );
}

{
/* initialize the memory segment */
int *smem = (int*) shmat( shm_id , NULL , 0 );
*smem = 0;
}

srand( ( unsigned int ) time( NULL ) );
int i = 0;
while( i < count ) {

int id;
if( !( id = fork() ) ) {
reader_writer( sem_id , shm_id );
}
else {
child[i++] = id;
sleep( 1 );
}
}

signal( SIGINT , handle );
while( 1 );

return EXIT_SUCCESS;
}
Nov 14 '05 #1
1 1888
Anand wrote:
I just implemented a program to solve reader writter prob. Wenever I
run the program it seems to go into S+ state and it waits there I dont
know why. When i attach gdb to one of the childs and step through for
some lines and dettach it runs for some time again ?????

The program is listed below. The listing is kind of
long


It's a lot shorter when all of the non-standard (and so off-topic) crap
is removed.
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: | last post by:
I am rewriting a C++ application in C#. This file has a combination of Text and Binary data. I used CFile before to read the text. If I hit a certain string that denotes the following data is...
1
by: Anand | last post by:
I just implemented a program to solve reader writter prob. Wenever I run the program it seems to go into S+ state and it waits there I dont know why. When i attach gdb to one of the childs and step...
3
by: Craig Lister | last post by:
Newbie alert! I am trying to get data from my mySQL database... With some help, I have got this far, but there is an exception in data.dll (??) on the : "OleDbDataReader Reader =...
2
by: Patrick Olurotimi Ige | last post by:
Why do i get "Invalid attempt to FieldCount when reader is closed" Is the problem the way the datareader reads data as opposed to a dataset? When trying to compile this code:- Dim reader As...
4
by: Ram | last post by:
I am using ADO.Net data reader to retrieve data from main frame. I am getting timestamp which is 26 (yyyy-mm-dd hh- mm-ss.123456) bytes as sqltimestamp data type. I am using data reader to...
12
by: Jerry Camel | last post by:
Not sure if this is a good place to post this... I'm writing and ASP.net app using vb .net. I need to interact with a credit card reader. I have one that sits inline with the keyboard. Works...
3
by: John | last post by:
Hi I am getting the System.IndexOutOfRangeException was unhandled error on the last line of below code; Cmd = New OleDb.OleDbCommand("SELECT FROM ", LocalConn) Reader = Cmd.ExecuteReader()...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, how do you resolve this problem? i have a public procedure in my DataAccessLayer that gets a SqlDataReader how do i close the reader from inside the DataAccessLayer if I'm returning...
1
by: EVH316 | last post by:
Hello Oracle DBA Guru, Is there anybody here had encoutered TNS Writter Failure error? Can you share what is the cause and the solution as well. Thanks...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.