-
syntax error Help!!
I was working on a simple example of IPC using the approach of Shared memory in C. And I have test it in the linux environment. It turned out that there could be some syntax problems with the code. I am not familiar with C programming language, and could not pinpoint the error of the code. Please help me. Any help would be appreciated. Thanks a lot. By the way, the error I got is near at the line containing main(). At least, that's what the compiler told me.
Server.c
Code:
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <data.h>
int main() {
//shared memroy segment ID
int segment_id;
//shared memory buffer
Message* buffer;
//Allocate a shared memory segment
segment_id = shmget(IPC_PRIVATE, sizeof(*buffer), S_IRUSR | S_IWUSR);
//Attach the shared memory segment
buffer = (Message*) shmat(segment_id, NULL, 0);
//print the shared memory segment id on the screen
print("Shared memory segment ID is %d\n", segment_id);
while (true) {
for(; ; ) {
sleep(100);
if(buffer->ID==0) break;
}
//process the message sent by the client
//Reverse the message string sent by the client
char* strpt = strrev(buffer->str);
//sent back the result to the client
buffer->str == strpt;
buffer->ID = 1;
if(buffer->ID==-1) {
//Detach the shared memory segment
shmdt(buffer);
//Remove the shared memory segment
shmctl(segment_id, IPC_RMID, NULL);
break;
}
}
return 0;
}
//Function to reverse a string
char* strrev(char* str) {
int length = strlen(str);
char* result = malloc(length + 1);
if( result != NULL ) {
result[length] = '\0';
for ( int i = length - 1, int j=0; i >= 0; i--, j++ )
result[j] = string[i];
}
return result;
}
client
Code:
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <data.h>
int main( ){
//shared memory segment ID
int segment_id;
//shared memory buffer
Message * buffer;
//Manual selection of segment ID
printf("Enter the shared memroy segment ID: ");
scanf("%d", &segment_id);
//Attach the shared memory segment
buffer = ( Message* ) shmat(segment_id, NULL, 0);
//Enter the Message to be sent to the client
while(true) {
printf("Enter the message you want to swap: ");
gets(buffer->str);
buffer->ID = 0;
while(true) {
//wait for the server to process the messa
sleep(100);
if(buffer->ID==1) break;
}
//display the message replied by the server
printf("The server reply: %s\n", buffer->str);
//Manual selection of quit the connection and exit
printf("Do you want to continue? Press Y to continue, N to exit.");
char* command;
gets(command);
if(command=="N"|| command=="n") break;
}
//Notify the server to close the connection
buffer->ID = -1;
//Detach the shared memory segment
shmdt(buffer);
return 0;
}
The Message is simply a struct
Code:
struct Mystruct {
/*Message status: 1-processed by the server.
0-not processed by the server.
-1-notify the server to close the connection. */
int ID;
//string to store the message string
char str[255];
}Message;
-
I know this is new to you... but when you're getting error messages it's generally a good idea to post them for us to see...
Also most compiler error messages are very explicit. They will have the filename and line number the error occurred on as well as a description of the error, so it's usually just a matter of actually reading what they say...
-
Quote:
Originally Posted by
liudaisuda
Server.c
Code:
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <data.h>
int main() {
//shared memroy segment ID
client
Code:
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <data.h>
int main( ){
The items in red are most likely your errors, e.g. something along the lines of "couldn't find data.h". The < and > designation tells the compiler to look in its system include folders to resolve the location of the header files. In your case, data.h is most likely something you created and hence should be referenced as #include "data.h". Additionally, take a read through How to define main()-FAQ
-
2 Attachment(s)
Thanks, I have fixed the problem. But here's two more errors with my code
Code:
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include "data.h"
int main( ){
//shared memory segment ID
int segment_id;
//shared memory buffer
Message * buffer = NULL;
//Manual selection of segment ID
printf("Enter the shared memroy segment ID: ");
scanf("%d", &segment_id);
//Attach the shared memory segment
buffer = ( Message* ) shmat(segment_id, NULL, 0);
//Enter the Message to be sent to the client
while(true) {
printf("Enter the message you want to swap: ");
gets(buffer->str);
buffer->ID = 0;
while(true) {
//wait for the server to process the messa
sleep(100);
if(buffer->ID==1) break;
}
//display the message replied by the server
printf("The server reply: %s\n", buffer->str);
//Manual selection of quit the connection and exit
printf("Do you want to continue? Press Y to continue, N to exit.");
char* command;
gets(command);
if(command=="N"|| command=="n") break;
}
//Notify the server to close the connection
buffer->ID = -1;
//Detach the shared memory segment
shmdt(buffer);
return 0;
}
Then the error is
Attachment 10889
While for the server file, I got the similar errors with one more. Please take a look at it, if you can
Code:
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include "data.h"
int main() {
//shared memroy segment ID
int segment_id;
//shared memory buffer
Message * buffer=NULL;
//Allocate a shared memory segment
segment_id = shmget(IPC_PRIVATE, sizeof(*buffer), S_IRUSR | S_IWUSR);
//Attach the shared memory segment
buffer = (Message * ) shmat(segment_id,NULL,0);
//print the shared memory segment id on the screen
print("Shared memory segment ID is %d\n", segment_id);
while (true) {
for(;;) {
sleep(100);
if(buffer->ID==0) break;
}
//process the message sent by the client
//Reverse the message string sent by the client
char* strpt = * strrev(buffer->str);
//sent back the result to the client
buffer->str == strpt;
buffer->ID = 1;
if(buffer->ID==-1) {
//Detach the shared memory segment
shmdt(buffer);
//Remove the shared memory segment
shmctl(segment_id, IPC_RMID, NULL);
break;
}
}
return 0;
}
//Function to reverse a string
char* strrev(char* str) {
int length = strlen(str);
char* result = malloc(length + 1);
if( result != NULL ) {
result[length] = '\0';
int i,j;
for ( i = length - 1, j=0; i >= 0; i--, j++ )
result[j] = str[i];
}
return result;
}
The corresponding error is
Attachment 10890
-
-
You couldn't wait 1 minute for a reply?
So what don't you understand? "undeclared" means you are trying to use a name of something without telling the compiler what that name is (you are pretending you have a variable when you don't). That usually means you forgot to make it, or you spelled it wrong. Conflicting types means you have the same name trying to be two different things.
Quzah.
-
This was originally posted in the more appropriate "Linux Programming" forum (http://cboard.cprogramming.com/linux...-ipc-help.html), where, it seems, they were also too impatient for a reply. Interestingly enough, I replied to that thread this morning, but it appears the post never went through. *sigh*. It went something like this:
@liudaisuda:
Please properly indent your code, so we can read it. Not that we really need to. You are severely lacking in some C fundamentals (like structs and typedefs, and string handling basics like don't compare strings with ==). You need to do some serious studying and really make sure you have a solid understanding of C programming before tackling something like shared memory. Sit down with some tutorials, like ours here, and some others you Google for, and even crack open a few books. Work through them all, doing all the examples. After that, the answers to your questions will be trivial. It wouldn't hurt if you had a cursory knowledge of Linux processes and Linux memory architecture as well.
You should also compile with your warnings set to the maximum. In gcc, this is the "-Wall" flag:
Code:
$ gcc -Wall client.c
client.c: In function ‘main’:
client.c:13: error: ‘buffer’ undeclared (first use in this function)
client.c:13: error: (Each undeclared identifier is reported only once
client.c:13: error: for each function it appears in.)
client.c:18: error: expected expression before ‘)’ token
client.c:26: warning: implicit declaration of function ‘sleep’
client.c:35: warning: comparison with string literal results in unspecified behavior
client.c:35: warning: comparison with string literal results in unspecified behavior
- Variables must be declared before they're used. This looks like a valid declaration, but really there's a problem in data.h.
- You need to #include the right file for sleep(). The documentation for sleep() will tell you what that is.
- You can't compare strings with ==, you need a function like strcmp()
Code:
$ gcc -Wall server.c
server.c: In function ‘main’:
server.c:13: error: ‘buffer’ undeclared (first use in this function)
server.c:13: error: (Each undeclared identifier is reported only once
server.c:13: error: for each function it appears in.)
server.c:17: error: expected expression before ‘)’ token
server.c:19: warning: implicit declaration of function ‘print’
server.c:22: warning: implicit declaration of function ‘sleep’
server.c:27: warning: implicit declaration of function ‘strrev’
server.c: At top level:
server.c:42: error: conflicting types for ‘strrev’
server.c:27: note: previous implicit declaration of ‘strrev’ was here
- Again, declare before you use, and again, it looks valid, but it's the result of the same problem in data.h
- The "expected expression" error is also because of the error in data.h
- print is not a valid function, printf is
- Again, find the correct include file for sleep()
- Move this above main(). C reads top-down and only knows about what it has already seen.
As for data.h?
Code:
struct MyStruct {
...
}Message;
This defines a global variable called Message, that is of type MyStruct. It does not make a type called "Message" that is a synonym for "struct MyStruct". For that you need to use typedef. Read up on how it works.