multithreading program help?
this is my problem....................
1. Within main(), read the contents of a file and store in a
single char array, compute the array length
The file name is got as a command line argument, use some
small file for 1000 – 2000 characters.
2. Create a message Queue, for use by the threads that will be
spawned below.
3. Now, spawn 11 threads, which will do the below activites.
– thread creation
1. the first 10 threads will read 1/10 th of the file content
from the array - use synchronization for this - mutex
read the 1/10th of the array and store in a local array
after reading the array, they will convert all upper case chars
to lower case – use toupper and all lower case chars to
upper case – use tolower
After converting to upper case, the modified local array
should be written into a message queue. – message queue
2. The 11th thread, will continuously poll for messages on
the queue, and whenever a message comes,it will read the
message and append it into the file "output.txt".
This thread should write "All thread processing over",
when all the 10 processing threads have finished processing
the data, and then return. – use a semaphore for this
4. The main program should exit when the 11th thread
(which writes into the file) completes – thread join
i wrote a code for this
Code:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/sem.h>
#include<semaphore.h>
#define NUM_CHARS 2000
#define MSGKEY 85
#define SEMKEY 1492
char buffer[2000];
char a[300];
int offset = 0;
int count = 0;
int semset_id;
struct sembuf sem_op;
union semun{
int val;
struct semid_ds *buf;
unsigned short *array;
};
struct msgbuf {
long type;
char *temp;
}snd,rcv;
pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
void *function1()
{
// msg snd;
// msg rcv;
int i;
int j=0;
FILE *fopen(),*output;
char *outputfile="output.txt";
char *b;
int local_count;
int msgid,x,y,k;
snd.type=1;
pthread_mutex_lock(&lock1);
local_count = strlen(buffer)/10;
pthread_mutex_unlock(&lock1);
//printf("%s",buffer);
b = (char *)malloc(local_count);
snd.temp = (char *)malloc(local_count);
printf("thread number: %ld\n",pthread_self());
for(i=offset;i<(local_count+offset);i++)
{
if((int)buffer[i]>64&&(int)buffer[i]<91)
{
a[i]=tolower(buffer[i]);
b[j]=tolower(buffer[i]);
// msg.buff[i]=a[i];
}
if((int)buffer[i]>96&&(int)buffer[i]<123)
{
a[i]=toupper(buffer[i]);
b[j]=toupper(buffer[i]);
// msg.buff[i]=a[i];
}
if(buffer[i]=='\n')
{
a[i]='\n';
b[j]='\n';
// msg.buff[i]=a[i];
}
j++;
}
offset = local_count+offset;
memcpy(snd.temp,b,local_count);
printf("temp array:%s",snd.temp);
free(b);
printf("%d\n",strlen(a));
// for(j=0;j<=(strlen(a));j++)
// {
// msg.buff[i]=a[i];
// }
msgid = msgget(MSGKEY, 0666|IPC_CREAT);
printf("msgid is :%d\n",msgid);
x = msgsnd(msgid,&snd,sizeof(snd),0);
if (x==-1)
printf("message sending failed \n");
else
output=fopen(outputfile,"a");
// fwrite(&temp,sizeof(temp),strlen(a),output);
// fclose(output);
printf("value returned:%d \n",x);
// free(snd.temp);
y = msgrcv(msgid,&rcv,sizeof(rcv),1,0);
if (y==-1)
printf("message receiving failed \n");
else
{
printf("no of bytes received = %d \n",y);
// printf("the message received is %s\n",rcv.temp[0]);
}
// msgctl(msgid,IPC_RMID,0);
printf("the contents of temporary array %s\n",a);
fflush(stdout);
output = fopen(outputfile,"a");
fwrite(&rcv,sizeof(rcv),50,output);
fclose(output);
sem_op.sem_num = 0;
sem_op.sem_op =1;
sem_op.sem_flg = 0;
semop(semset_id,&sem_op,1);
pthread_exit(NULL);
// pthread_mutex_unlock(&lock1);
}
void *function()
{
sem_op.sem_num = 0;
sem_op.sem_op = -10;
sem_op.sem_flg = 0;
semop(semset_id,&sem_op,1);
printf("All threads processing over..");
pthread_exit(NULL);
}
//struct msgbuf
//{
// long type;
// char buff[2000];
//};
int main()
{
FILE *fpopen(),*fp;
int i,j;
int x[300];
int y[300];
char ch;
char filename[50];
union semun sem_val;
void *ret_val;
int pid;
unsigned int len;
pthread_t thread[10],thread11;
printf("Enter the file to be displayed:");
gets(filename);
fp = fopen(filename,"r");
while(fp==NULL)
{
printf("file %s doesnot exist\n",filename);
printf("Enter the correct file name:");
gets(filename);
fp = fopen(filename,"r");
}
for(i=0;(i<NUM_CHARS)&&(feof(fp)==0);i++)
{
ch = fgetc(fp);
buffer[i] = ch;
}
len = strlen(buffer);
printf("number of characters in the array is %u\n",len);
// printf("contents of the file are %s \n",buffer);
fclose(fp);
semset_id = semget(IPC_PRIVATE,1,0600);
sem_val.val = 0;
semctl(semset_id,0,SETVAL,sem_val);
for(j=0;j<10;j++)
{
pthread_create(&thread[j],NULL,&function1,NULL);
}
pthread_create(&thread11,NULL,function,NULL);
// for(j=0;j<10;j++)
// pthread_join(thread[j],&ret_val);
pthread_join(thread11,NULL);
// fclose(output);
}
but there is some problem with the code while appending its showing some garbage value along with the strings.......................
i want the contents of output file to be same as that of input file and also i am trying to use the above concepts threads,messagequeues,semaphores
thanx for help in advance