Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // server.c
- //
- // A QNX msg passing server. It should receive a string from a client,
- // calculate a checksum on it, and reply back the client with the checksum.
- //
- // The server prints out its pid and chid so that the client can be made aware
- // of them.
- //
- // Using the comments below, put code in to complete the program. Look up
- // function arguments in the course book or the QNX documentation.
- ////////////////////////////////////////////////////////////////////////////////
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/neutrino.h>
- #include <sys/netmgr.h>
- #include <process.h>
- #include <string.h>
- #include <unistd.h>
- #define SIZE 100
- #define THREADS 10
- typedef struct {
- int typ; // Typ komunikatu
- int od; // Numer procesu
- char tekst[SIZE]; // Tekst komunikatu
- } kom_t;
- int main(void) {
- int chid;
- int pid;
- int i;
- int donethreads = 0;
- chid = ChannelCreate(0); //PUT CODE HERE to create a channel
- if(-1 == chid) { //was there an error creating the channel?
- perror("ChannelCreate()"); //look up the errno code and print
- exit(EXIT_FAILURE);
- }
- pid = getpid(); //get our own pid
- printf("Server's pid: %d, chid: %d\n", pid, chid); //print our pid/chid so
- //client can be told where to
- //connect
- for(i = 0; i < THREADS; ++i) {
- if(fork() == 0) {
- int reply, coid;
- kom_t s_msg;
- coid = ConnectAttach(ND_LOCAL_NODE, pid, chid, _NTO_SIDE_CHANNEL, 0);
- if(coid > 0) {
- int j;
- for(j = 0; j < 9; ++j) {
- s_msg.typ = 0;
- s_msg.od = getpid();
- strcpy(s_msg.tekst, "Wiadomosc");
- MsgSend(coid, &s_msg, sizeof(s_msg), &reply, sizeof(reply));
- }
- s_msg.typ = 1;
- s_msg.od = getpid();
- strcpy(s_msg.tekst, "Koniec");
- MsgSend(coid, &s_msg, sizeof(s_msg), &reply, sizeof(reply));
- return EXIT_SUCCESS;
- } else {
- perror("ConnectAttach()");
- return EXIT_FAILURE;
- }
- }
- }
- int rcvid;
- int reply = 1;
- kom_t rc_msg;
- do {
- rcvid = MsgReceive(chid, &rc_msg, sizeof(rc_msg), NULL);
- printf("Typ: %d, od: %d, tekst: %s\n", rc_msg.typ, rc_msg.od, rc_msg.tekst);
- MsgReply(rcvid, EOK, &reply, sizeof(reply));
- if(rc_msg.typ) {
- ++donethreads;
- }
- } while (donethreads != THREADS);
- int status;
- for(i = 0; i < 10; ++i) {
- wait(&status);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement