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);
}