Hi, I would linke to know what is the best way to synchronise the shared memory segment. is threads usage are also advisible whie synhcronising the shared memory segement. in what circumstances we should use threads or semaphore to synchronize the memory segment.

Member Avatar
Member Avatar
+0 forum 3

In the following example two threads share int counter. The increment_thread thread increases counter by one . The decrement_thread thread decreases counter by one . I use semaphore to control access to counter instead of making the two methods synchronized. Is this the proper way of using semaphore? When should we use synchronized methods and when a semaphore?(we don't want more than one thread to access counter at the same time). If both are proper , which is preferable? import java.util.concurrent.Semaphore; public class SemaphoreMultithreadingExample { static int counter = 0; static Semaphore semaphore = new Semaphore(1); public static void incrementCounter(){ …

Member Avatar
Member Avatar
+0 forum 2

Hi members, This is a program i wanted to implement semaphore between parent and child processes which are trying to access a shared variable called counter ... i dont know why it is not getting incremented during the child process !! someone help please !! #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <semaphore.h> #include <stdlib.h> #include <errno.h> #include <string.h> sem_t mutex; int counter; int main () pid_t child_pid; sem_init(&mutex, 0, 1); printf ("the main program process ID is %d\n", (int) getpid ()); child_pid = fork (); if (child_pid != 0) { printf ("This is the parent process, with id %d\n", …

Member Avatar
Member Avatar
+0 forum 2

Hi, I have two threads A and B. Thread A prints only odd numbers. Thread B prints only even number. I want to synchronize it in such a way that on the screen I get continues numbers. How can I do that? I was thinking of using sockets. Thread A prints and sends a message to thread B. Thread B prints and then sends a message to thread A. Any thread before printing checks for the message from other thread and prints only if they have received the message. Is this the best design or any of you have a …

Member Avatar
Member Avatar
+0 forum 3

hi friends I've a code to compute integral with area function in code. I used Fork, and for returned values I must use shared memory. now I need to embed a semaphore in my code to control the critical area(here is shared memory). I don't know how to embed semaphore in it. Here is the code: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> #include<sys/types.h> #include<sys/shm.h> #include<semaphore.h> int area(int a,int b,int H) {return (((a*a)+(b*b))*H)/2;} sem_t sem; main() { int *value; pid_t pid; int i; int status; int result=0; int pids[10]; int shmid; key_t key; key=ftok("prog.c",0); shmid=shmget(key,1024,0644|IPC_CREAT); value=shmat(shmid,(void *)0,0); if(value==(int *)(-1)) perror("shmat"); if(sem_init(&sem,0,0)==-1) perror("Semaphore initialize …

Member Avatar
Member Avatar
+0 forum 1

Hey guys I'm going through some questions, studying for exams and below is one which has had me stressing all afternoon. Consider a uniprocessor system executing concurrently two processes P and Q. Each process executes the code listed below, process P – procedure P, and process Q – procedure Q. Both processes arrive within a very short time of each other, but no assumptions can be made about the time they start execution and their relative speed. All statements used in the code below from A to K are atomic ie. they either execute completely or not at all. The …

Member Avatar
Member Avatar
+0 forum 1

Can I use semaphore or mutex on MikroC? If you know , please tell me the function name or exsample code.

Member Avatar
Member Avatar
+0 forum 1

Hey all, I was hoping I could get some advice on designing the proper threading for our project. A little background info - our company does simulators of complex electrical/mechanical systems. These simulators could have fuel systems, electrical systems, pneumatic pressure systems, etc. I suppose you can just think of a car and that's close enough. We have done many of these simulators and have no problems with the systems themselves. However, we get requests from customers that have drastically different parts. Think of simulating one model of a car, including the ability to fail every individual component and system, …

Member Avatar
Member Avatar
+0 forum 2

[CODE] sem_t w,r,s1,s2,s3; pthread_mutex_t m; int main(int argc, char* argv[]) { mutex and semaphores initialization ..... call threads.... wait until finish and cancel threads... clean threads and semaphores [I]exit;[/I] } void *reader(void *argv) { while(TRUE) { sem_wait(&r); sem_wait(&s1); pthread_mutex_lock(&m); ... read data..... pthread_mutex_unlock(&m); sem_post(&s1); sem_post(&w); } printf("Done read\n"); sem_post(&extra2); pthread_exit(NULL); } void *writer(void *argv) { while(TRUE) { sem_wait(&w); sem_wait(&s1); pthread_mutex_lock(&m); pthread_mutex_unlock(&m); sem_post(&s1); sem_post(&r); } printf("Done Write\n"); sem_post(&s3); pthread_exit(NULL); } [/CODE] I wrote a reader/writer issue and now it's writing the same as from the source ... but sometime it become unstable and making output wrong or stop working.. Can someone …

Member Avatar
Member Avatar
+0 forum 1

I'm trying to do the third writer/reader problem with semaphore (as mention on wiki). But it's not working properly. First 16-element array seem to work properly. But after first, it got wrong. Even though I think I'm almost there, I can't find the error at all. My code is as the following : [CODE] #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <semaphore.h> #include <string.h> #include <fcntl.h> #define TRUE 1 #define MAX 16 #define STR_MAX 200 void *writer(void*); void *reader(void*); void freebuff(char*); char *init(char* , int); void print_buff(); int readCount = 0; sem_t mutex ,write, read; char* CirBuf[MAX]; int main(int argc, …

Member Avatar
+0 forum 0

I don't get it why my threads can't perform properly for writer/reader problem. I think something with the semaphore. but can't solve and online can't help me too. [CODE] semaphore mutex, read, write; void *reader(void *argv) { char buffer[200]; int readNo, i=0; do { sem_wait(&read); sem_wait(&mutex); sem_getvalue(&read, &readNo); fgets(buffer, sizeof(buffer), argv); //read line by line from file CirBuf[i] = init(CirBuf[i], strlen(buffer)); print_buff(); CirBuf[i] = buffer; i++; if(i == 15) i = 0; sem_post(&mutex); sem_post(&write); }while(readNo > 0); sem_post(&write); pthread_exit(NULL); } void *writer(void *argv) { int i= 1; do { sem_wait(&write); sem_wait(&mutex); sem_getvalue(&write, &i); printf("arrive here\n"); if(CirBuf[i] != NULL) { fputs(CirBuf[i], …

Member Avatar
+0 forum 0

My understanding of this subject is - Two or more processes accessing a semaphore concurrently can cause Deadlock The provelms with semaphores is that you can forget to call the release method and it can cause deadlock Starvation Both processes can change the P and V counters of the semaphore and by doing put the process in an inconsistenet state. Is there anything anyone can add to this or have i just confused myself too much

Member Avatar
Member Avatar
+0 forum 6

The End.