LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-18-2009, 01:06 AM   #1
jkeertir
Member
 
Registered: Mar 2008
Posts: 70

Rep: Reputation: 15
thread synchronization


Dear all,

I am new to thread programming.Please let me know where am i going wrong.
I have two files
1)alphabet.txt
aaaaaaaaa
bbbbbbbbb
ccccccccc
ddddddddd

and

2)number.txt

111111111111
2222222222222
3333333333333
444444444444


i want the output as
aaaaaaaaa
111111111111
bbbbbbbbb
2222222222222
ccccccccc
3333333333333
ddddddddd
444444444444


but i am getting it as
aaaaaaaaa
bbbbbbbbb
ccccccccc
ddddddddd

111111111111



here is my code

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
void *starting_point(void *);
struct node
{
char data[80];
struct node *next;
};
sem_t sem;

int main()
{
pthread_t thread1;
FILE *fp1;
int ret;
char buff1[80];
int read1=1;
sem_init(&sem,0,0);
//sem_post(&sem);
ret=pthread_create(&thread1,NULL,starting_point,NULL);
printf("the ret value is %d",ret);
fp1=fopen("/home/keerti/number.txt","r");
sem_post(&sem);
if(fp1==NULL)
{
printf("cannot open number.txt");
//exit(1);
}

while(fgets(buff1,79,fp1)!=NULL)
{
sem_wait(&sem);
printf("the line no is %d buff1 is %s",read1,buff1);
read1++;
//pthread_join(thread1,NULL);
//sem_wait(&sem);
//sem_post(&sem);
}
fclose(fp1);
}


void *starting_point(void *arg)
{
char buff[80];
FILE *fp;
int read=1;
//sem_wait(&sem);
fp=fopen("/home/keerti/alphabet.txt","r");
if(fp==NULL)
{
printf("cannot open the file");
}
while(fgets(buff,79,fp)!=NULL)
{
//sem_wait(&sem);
printf("bytes read is %d buff is %s",read,buff);
read++;
//sem_post(&sem);
}
fclose(fp);

}
 
Old 09-18-2009, 03:10 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Threads are non-deterministic. If you want to print out lines alternately from 2 different files you can do that in a normal single threaded process.
 
Old 09-18-2009, 03:17 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Jessie (Fluxbox WM)
Posts: 1,388
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
It isn't clear why you are using threads to do this?

If you have to do it with semaphores, then you would need two semaphores, each one granting the other task its turn (so that they take it in turns). Using only one semaphore, it is not determinable which task will get access (typically the first task will just use up its timeslice and complete everything before the second task gets a look in). You have also put the join in the wrong place (you are calling join inside the loop instead of at the end).

Alternatively you could use an access semaphore (so that they don't write to the output at the same time), and a pthread_barrier to synchronize them as they loop through the lines of the files. This is a less portable method (ie, most systems have semaphores, but not all have a barrier primitive).
 
  


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
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 12:28 PM
Thread Synchronization in Linux deepthisundareshan Linux - General 2 04-09-2009 12:08 AM
thread synchronization assignment help!! rossi143 Programming 2 03-25-2007 12:38 PM
thread Synchronization ej25 Programming 12 12-24-2004 02:11 PM
email synchronization (not file synchronization) Moebius Linux - Software 6 10-05-2004 06:31 AM


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