LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-18-2013, 02:15 PM   #1
pratham29
LQ Newbie
 
Registered: Oct 2013
Posts: 8

Rep: Reputation: Disabled
Mutex Unlock


Hi,
I am trying to execute the following code but I am getting a bash error:Access denied comment for following mutex lock/unlock program.
<CODE>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex_1;

int counter ;

void *child1(void *arg)
{
while(1){
pthread_mutex_lock(&mutex_1);
sleep(1);
if(counter > 25)
pthread_exit(NULL);
else
counter++;
pthread_mutex_unlock(&mutex_1);
printf("Child1: counter=%d\n", counter);
}
}


int main(void)
{
pthread_t tid1;

counter=0;
pthread_mutex_init(&mutex_1,NULL);
pthread_create(&tid1,NULL,child1,NULL);
do{
pthread_mutex_lock(&mutex_1);
sleep(1);
counter++;
pthread_mutex_unlock(&mutex_1);
printf("Main: counter=%d\n", counter);

}while(1);
pthread_exit(0);
}
</CODE>

Thanks for your time and consideration,awaiting response.
Regards,
Pratham
 
Old 11-19-2013, 02:46 PM   #2
youngstorm
Member
 
Registered: May 2003
Location: USA
Distribution: Fedora 21, RHEL 5,6&7, Windoze 7
Posts: 235

Rep: Reputation: 31
Hi. It runs OK for me, at least, I think it does. Here is the output from running it 3 times. Twice as root and once as a user. Is this the output you expected?

Quote:
[root@computer ~]# cc -pthread -o test test.c
[root@computer ~]# ./test
Main: counter=1
Child1: counter=2
Main: counter=3
Main: counter=4
Main: counter=5
Main: counter=6
Child1: counter=7
Main: counter=8
^C
Quote:
[root@computer ~]# ./test
Main: counter=1
Child1: counter=2
Main: counter=3
Main: counter=4
Main: counter=5
Main: counter=6
Main: counter=7
Main: counter=8
Main: counter=9
Main: counter=10
Main: counter=11
Main: counter=12
Main: counter=13
Main: counter=14
Main: counter=15
Main: counter=16
Main: counter=17
Main: counter=18
Main: counter=19
Main: counter=20
Main: counter=21
Main: counter=22
Main: counter=23
Child1: counter=24
Child1: counter=25
Child1: counter=26
^C

Quote:
[user@computer ~]$ cc -pthread -o test test.c
[user@computer ~]$ ./test
Main: counter=1
Child1: counter=2
Main: counter=3
Main: counter=4
Main: counter=5
Main: counter=6
Main: counter=7
Main: counter=8
Main: counter=9
Main: counter=10
Main: counter=11
Main: counter=12
Main: counter=13
Main: counter=14
Main: counter=15
Main: counter=16
Main: counter=17
Main: counter=18
Main: counter=19
Main: counter=20
Main: counter=21
Main: counter=22
Main: counter=23
Main: counter=24
Main: counter=25
Main: counter=26
Main: counter=27
Main: counter=28
Main: counter=29
Main: counter=30
^C
Also, please show me the exact compile and run commands you used.


Mike

Last edited by youngstorm; 11-19-2013 at 03:18 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
mutex vs semaphore vishnu anand Programming 1 12-19-2011 04:41 PM
Mutex v/s Semaphore jayadhanesh Linux - Software 3 12-07-2010 01:20 AM
will mutex unlock cause context switch? jackandking Linux - Kernel 4 03-30-2009 02:40 AM
Mutex new2lunix Programming 1 12-02-2008 09:12 PM
Mutex attributes? estratos Programming 12 12-24-2006 04:54 AM


All times are GMT -5. The time now is 04:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration