I am trying to learn read/write programs in C and I found an example online that I thought I should start studying. When I tried compiling the code though, I get a few warnings and one error like this:
read.c:21:8: error: conflicting types for ‘write’
void * write(void *temp) {
^
In file included from read.c:11:0:
/usr/include/unistd.h:369:16: note: previous declaration of ‘write’ was here
extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur;
^
read.c: In function ‘write’:
read.c:25:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
ret=pthread_rwlock_wrlock(&rwlock);
^
read.c: In function ‘write_2’:
read.c:45:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
ret=pthread_rwlock_wrlock(&rwlock);
^
read.c: At top level:
read.c:106:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main() {
I'm still very new to C programming and I couldn't figure out how to get these warnings and error solved, since I just jumped into this topic without knowing the language too well. Here's the code I'm trying to compile.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
/*
From the output we can see that the two writes were executed one after the other. But in case of reads, even though read_1 had not unlocked the rwlock, read_2 was allowed into the critical section and read the file. That shows us that multiple readers are allowed but only one writer is allowed into the critical section.
*/
pthread_rwlock_t rwlock; // allows multiple readers to access the resource, but only one reader at any given time.
void * write(void *temp) {
char *ret;
FILE *file1;
char *str;
ret=pthread_rwlock_wrlock(&rwlock);
printf("\nFile locked, please enter the message \n");
str=(char *)malloc(10*sizeof(char));
file1=fopen("temp","w");
scanf("%s",str);
fprintf(file1,"%s",str);
fclose(file1);
pthread_rwlock_unlock(&rwlock);
printf("\nUnlocked the file you can read it now \n");
return ret;
}
void * write_2(void *temp) {
char *ret;
FILE *file1;
char *str;
sleep(3);
ret=pthread_rwlock_wrlock(&rwlock);
printf("\nFile locked, please enter the message \n");
str=(char *)malloc(10*sizeof(char));
file1=fopen("temp","a");
scanf("%s",str);
fprintf(file1,"%s",str);
fclose(file1);
pthread_rwlock_unlock(&rwlock);
printf("\nUnlocked the file you can read it now \n");
return ret;
}
void * read_1(void *temp) {
char *ret;
FILE *file1;
char *str;
sleep(5);
pthread_rwlock_rdlock(&rwlock);
printf("\n1 Opening file for reading\n");
file1=fopen("temp","r");
str=(char *)malloc(10*sizeof(char));
fscanf(file1,"%s",str);
printf("\nMessage from file is %s \n",str);
sleep(3);
fclose(file1);
printf("\nUnlocking rwlock\n");
pthread_rwlock_unlock(&rwlock);
return ret;
}
void * read_2(void *temp) {
char *ret;
FILE *file1;
char *str;
sleep(6);
pthread_rwlock_rdlock(&rwlock);
printf("\n2 Opening file for reading\n");
file1=fopen("temp","r");
str=(char *)malloc(10*sizeof(char));
fscanf(file1,"%s",str);
printf("\nMessage from file is %s \n",str);
fclose(file1);
pthread_rwlock_rdlock(&rwlock);
return ret;
}
main() {
pthread_t thread_id,thread_id1,thread_id3,thread_id4;
pthread_attr_t attr;
int ret;
void *res;
pthread_rwlock_init(&rwlock,NULL);
ret=pthread_create(&thread_id,NULL,&write,NULL);
ret=pthread_create(&thread_id1,NULL,&read_1,NULL);
ret=pthread_create(&thread_id3,NULL,&read_2,NULL);
ret=pthread_create(&thread_id4,NULL,&write_2,NULL);
printf("\n Created thread");
pthread_join(thread_id,&res);
pthread_join(thread_id1,&res);
pthread_join(thread_id3,&res);
pthread_join(thread_id4,&res);
pthread_rwlock_destroy(&rwlock);
}
Question is why are these warnings and this error showing up for this code?
P.S. not sure why but when I try to run it after getting these warning and errors, it still runs the way I thought it would. It's quite odd. Thank you for reading.
write
is the name of a system call. The declaration for the function is present in <unistd.h>
which you have included into your C program.
Your C program continues to work because nowhere are you using the actual write
system call; the C standard library uses it but it is linked to that other write
function statically.
As for the other warnings,
main()
is not proper C at all. It needs to have prototype of either int main(void)
or int main(int argc, char *argv[])
.
For pthread_*
functions you must #include <pthread.h>
.
The return value of pthread_rwlock_wrlock
is not a pointer, but int
, so you must assign it to an object of type int
, yet you assign it to ret
which is of type char *
All in all, you should turn on all generally helpful diagnostics in the C compiler (-Wall
), and consider each warning an error (-Werror
).
Finally, all compilation and linking command lines should have the -pthread
flag on them.