PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : seltsames scanf unter c.


noid
2004-06-12, 10:10:18
also ich weiss nicht warum das hier nicht funktioniert:
nach eingabe eines zeichens passiert nix, außerdem reagiert er etwas seltsam auf ne wiederholte eingabe.


#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MSGSIZE 100

main(void)
{
int sqid, rqid, key, pid, op1 = 0, op2 = 0, erg;
char opc;
struct msgbuf {
long mtype;
char mtext[MSGSIZE];
} buf;

pid = (long) getpid();

puts("Welcome to mclient");

sqid = msgget((key_t)1111, 0660);
if (sqid == -1)
{
perror("Fehler: ");
exit(1);
}
rqid = msgget((key_t)2222, 0660);
if (rqid == -1)
{
perror("Fehler: ");
exit(1);
}

do
{
printf("Ausgabe: Auftrag eingeben\nEingabe: ");
scanf("%c", &opc);
if (opc == 'x' || opc == 'X') exit(0);
scanf("%d %d", &op1, &op2);

sprintf(buf.mtext,"%d %c %d %d", pid, opc, op1, op2);
buf.mtype = (long)1;

//printf("Zu sendende Nachricht: "); puts(s_buf.mtext);
if (msgsnd(sqid,&buf, strlen(buf.mtext)+1, 0) == -1)
{
printf("Fehler: msgsnd failed\n");
exit(1);
}
//puts("Nachricht gesendet");

if (msgrcv(rqid,&buf, MSGSIZE, (long)pid, 0) == -1)
{
printf("Fehler: msgrcv failed\n");
exit(1);
}
sscanf(buf.mtext, "%d", &erg);
printf("Ausgabe: %d\n", erg);
} while(1);

}


dann bin ich bei dieser lösung gelandet, weil ich das gefühl hab mir machen die whitespaces ein strich durch die rechnung (der code geht auch 1a):


#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MSGSIZE 100

main(void)
{
int sqid, rqid, key, pid, op1 = 0, op2 = 0, erg;
char opc[1];
struct msgbuf {
long mtype;
char mtext[MSGSIZE];
} buf;

pid = (long) getpid();

puts("Welcome to mclient");

sqid = msgget((key_t)1111, 0660);
if (sqid == -1)
{
perror("Fehler: ");
exit(1);
}
rqid = msgget((key_t)2222, 0660);
if (rqid == -1)
{
perror("Fehler: ");
exit(1);
}

do
{
printf("Ausgabe: Auftrag eingeben\nEingabe: ");
scanf("%s", opc);
if (*opc == 'x' || *opc == 'X') exit(0);
scanf("%d %d", &op1, &op2);

sprintf(buf.mtext,"%d %c %d %d", pid, *opc, op1, op2);
buf.mtype = (long)1;

//printf("Zu sendende Nachricht: "); puts(s_buf.mtext);
if (msgsnd(sqid,&buf, strlen(buf.mtext)+1, 0) == -1)
{
printf("Fehler: msgsnd failed\n");
exit(1);
}
//puts("Nachricht gesendet");

if (msgrcv(rqid,&buf, MSGSIZE, (long)pid, 0) == -1)
{
printf("Fehler: msgrcv failed\n");
exit(1);
}
sscanf(buf.mtext, "%d", &erg);
printf("Ausgabe: %d\n", erg);
} while(1);

}

noid
2004-06-12, 10:12:11
ach, hier der code des "servers". falls jemand das mal testen möchte.


#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>

#define MSGSIZE 100

void cleanUp(int sig)
{
int rqid, sqid;
if (sig == 2 || sig == 15) {
if (sig == SIGTERM) puts("SIGTERM empfangen");
else puts("SIGINT empfangen");

puts("CleanUp: Öffne receive-queue");
rqid = msgget((key_t)1111, 0);
puts("CleanUp: Lösche receive-queue");
if (msgctl(rqid, IPC_RMID, NULL) == -1) {
perror("Fehler: ");
}

puts("CleanUp: Öffne send-queue");
sqid = msgget((key_t)2222, 0);
puts("CleanUp: Lösche send-queue");
if (msgctl(sqid, IPC_RMID, NULL) == -1) {
perror("Fehler: ");
}

puts("Server: Tot");
exit(0);
}
}

void *calc(void* arg) {
int pid, sqid, op1, op2, erg;
char opc;
struct msgbuf {
long mtype;
char mtext[MSGSIZE];
} s_buf;

//puts("Thread aktiv");
sqid = msgget((key_t)2222, 0660);
sscanf((char*)arg, "%d %c %d %d", &pid, &opc, &op1, &op2);

switch (opc) {
case '+' : erg = op1 + op2;
break;
case '-' : erg = op1 - op2;
break;
default : erg = 0;
}

//printf("%d %c %d = %d\n", op1, opc, op2, erg);

s_buf.mtype = pid;
sprintf(s_buf.mtext, "%d", erg);
msgsnd(sqid,&s_buf, strlen(s_buf.mtext)+1, 0);

//puts("Thread endet");
pthread_exit(NULL);
}

main(void)
{
int sqid, rqid, key, pid;
pthread_t tid;
char ch[MSGSIZE-sizeof(long)];

signal(SIGTERM, cleanUp);
signal(SIGINT, cleanUp);

struct msgbuf {
long mtype;
char mtext[MSGSIZE];
} r_buf;

printf("Welcome to mserver\n");

puts("Init: Erstelle receive-queue");
rqid = msgget((key_t)1111, 0660 | IPC_CREAT | IPC_EXCL);
if (rqid == -1) {
perror("Fehler: ");
cleanUp(2);
exit(1);
}
puts("Init: Erstelle send-queue");
sqid = msgget((key_t)2222, 0660 | IPC_CREAT | IPC_EXCL);
if ( sqid == -1) {
perror("Fehler: ");
cleanUp(2);
exit(1);
}
puts("Warte auf orbeit....");

do {
if (msgrcv(rqid,&r_buf, MSGSIZE, (long)1, 0) == -1)
{
perror("Fehler: msgrcv failed\n");
} else {
strcpy(ch, r_buf.mtext);
pthread_create(&tid, NULL, calc, ch);
}
}
while(1);

}


passendes makefile:



all: mserver mclient

mserver: mserver.c
gcc mserver.c -lpthread -o mserver
mclient: mclient.c
gcc mclient.c -o mclient


...soviel zu ipc mit linux.
(der thread war nicht nötig, aber so als spielerei)