コード例 #1
0
/**
 * Simple test to verify ability to send and recieve messages
 * @return [description]
 */
int test_send_message(void) {
	int childPID = fork();

	if(childPID == 0) {
		// Variables to hold message data
		pid_t sender;
		void *msg[MAX_MSG_SIZE];
		int len;
		// Try to get message
		int status_c = RcvMsg(&sender, msg, &len, true);

		char myMesg[] = "I am your child";
		int error = SendMsg(sender, myMesg, 16, true);
		if(error) return 0;
		exit(0);
	} else {
		// Variables to hold message data
		pid_t sender;
		void *msg[MAX_MSG_SIZE];
		int len;
		char mesg[] = "I am your father";
		int status_p = SendMsg(childPID, mesg, 17, false);
		if (status_p) return 0; // If status is non-zero, there was an error
		status_p = RcvMsg(&sender, msg, &len, true);
		if(status_p) return 0; // If status is non-zero, there was an error
		return 1;
	}
}
コード例 #2
0
/**
 * Tests that we are recieveing messages in first-in-first-out, even if there's an error while reading once.
 * @return [description]
 */
int fifo_even_if_errors() {
    pid_t parent = getpid(), child = fork();

    if (child) {
        // in parent
        usleep(50000);

        pid_t sender;
        int msg, len, i, error;
        error = RcvMsg(NULL, NULL, NULL, BLOCK);
        expect_true(error == MSG_ARG_ERROR);

        for (i = 0; i < 32; i++) {
            error = RcvMsg(&sender,&msg,&len,BLOCK);
            expect_true(error == 0);
            expect_true(len == sizeof(int));
            //printf("expected %d, got %d\n", i, msg);
            expect_true(msg == i);
        }
    } else {
        // in child
        int i;
        for (i = 0; i < 32; i++) {
            SendMsg(parent, &i, sizeof(int), NO_BLOCK);
        }
        exit(0);
    }
}
コード例 #3
0
ファイル: testmailbox2.c プロジェクト: mracine/KernelMailbox
int main() {
  int childCounter;
  
  for(childCounter = 0; childCounter < CHILD_NUM; childCounter++) {
    int childPID = fork();
    
    if(childPID == 0){
      pid_t sender;
      void *msg[128];
      int len;
      bool block = true;
      
      sleep(0.1);

      RcvMsg(&sender,msg,&len,block);
      
      printf("Message: %s\n", (char *)msg);
      char myMesg[] = "I am your child";

      if(SendMsg(sender, myMesg, 16, block)) {
        printf("Child send failed.\n");
      }
      
      return 0;
    }

    else{
      char mesg[] = "I am your father";

      if (SendMsg(childPID, mesg, 17, false)){
        printf("Send failed\n");
      }

      wait(&childPID);
    }
  }
  
  int msgCounter;
  for(msgCounter = 0; msgCounter < CHILD_NUM; msgCounter++) {
    pid_t aSender;
    void *reply[128];
    int mLen;
    bool mBlock = true;

    sleep(0.1);
    
    RcvMsg(&aSender,reply,&mLen,mBlock);
    printf("Child %d, enqueued # %d Message: %s\n", aSender, msgCounter, (char *)reply);
  }

  return 0;
}
コード例 #4
0
int main() {
  pid_t sender;
  sender = getpid();
  int childPID = fork();
  int status;
  
  if(childPID == 0){
    void *msg[128];
    int len;
	int count;
    ManageMailbox(false, &count);
    if(status= RcvMsg(&sender,msg,&len,false))
    {printf("ERROR: %d\n", status);}
	else 
	{
		printf("Message received.\n");
		printf("Sender: %d\nMessage: %s\nLen: %d\n", sender, (char *) msg, len);
	}
	if(status= RcvMsg(&sender,msg,&len,false))
	{printf("ERROR: %d\n", status);}
	else 
	{
		printf("Message received.\n");
		printf("Sender: %d\nMessage: %s\nLen: %d\n", sender, (char *) msg, len);
	}
	if(status= RcvMsg(&sender,msg,&len,false))
	{printf("ERROR: %d\n", status);}
	else 
	{
		printf("Message received.\n");
		printf("Sender: %d\nMessage: %s\nLen: %d\n", sender, (char *) msg, len);
	}
  }
  else{
    char mesg[] = "I am your father";
    char mesg2[] = "Join me in the dark side!";
    char mesg3[] = "Together we can rule the galaxy!";
    printf("Sending Messages to child.\n");
    if (status=SendMsg(childPID, mesg, 17, false)){
      printf("Send failed with error %d\n", status);
    }
    if (status=SendMsg(childPID, mesg2, 26, false)){
      printf("Send failed with error %d\n", status);
    }
    if (status=SendMsg(childPID, mesg3, 33, false)){
      printf("Send failed with error %d\n", status);
    }
  }
  return 0;
}
コード例 #5
0
/**
 * Test recieving on an empty mailbox
 * @return [description]
 */
int test_recieve_empty_mailbox(void) {
	// Variables to hold message data
	pid_t sender;
	void *msg[MAX_MSG_SIZE];
	int len;
	// Try to get message
	int error = RcvMsg(&sender, msg, &len, false);

	return error == MAILBOX_EMPTY;
}
コード例 #6
0
int msg_len_errors() {
    pid_t me = getpid();
    char msg1[MAX_MSG_SIZE+1];
    char msg2[MAX_MSG_SIZE];
    int i;
    for (i = 0; i < MAX_MSG_SIZE; i++) {
        msg2[i] = 'g' + i;
    }
    char msg2_rcv[MAX_MSG_SIZE];
    char msg3[1];
    msg3[0] = 'h';

    int error = SendMsg(me,msg1,MAX_MSG_SIZE+1,NO_BLOCK);
    expect_true(error == MSG_LENGTH_ERROR);

    error = SendMsg(me,msg2,MAX_MSG_SIZE,NO_BLOCK);
    expect_true(error == 0);

    int len;
    error = RcvMsg(&me, msg2_rcv, &len, NO_BLOCK);
    expect_true(error == 0);
    expect_true(me == getpid());
    expect_true(len == MAX_MSG_SIZE);
    for (i = 0; i < MAX_MSG_SIZE; i++) {
        expect_true(msg2[i] == msg2_rcv[i]);
    }

    error = SendMsg(me, msg3, 1, NO_BLOCK);
    expect_true(error == 0);

    char msg3_rcv[MAX_MSG_SIZE];
    error = RcvMsg(&me, msg3_rcv, &len, NO_BLOCK);
    expect_true(len == 1);
    expect_true(len != 2);
    expect_true(msg3_rcv[0] == msg3[0]);

    error = SendMsg(me, "Hello", -1, NO_BLOCK);
    expect_true(error = MSG_LENGTH_ERROR);

    return true;
}
コード例 #7
0
void * subthread_dorecieve(void* args) {
    pid_t sender;
    void* msg = malloc(MAX_MSG_SIZE);
    int len;

    int* error = malloc(sizeof(int));

    *error = RcvMsg(&sender, msg, &len, BLOCK);
    free(msg);

    return error;
}
コード例 #8
0
/**
 * Tests if programs that chose to wait until able to send a message behave properly
 * @return [description]
 */
int test_message_overflow_wait(void) {
	int childCounter;
	int childPID;
	for(childCounter = 0; childCounter < CHILD_NUM; childCounter++) {
		childPID = fork();
		
		if(childPID == 0){
			pid_t sender;
			void *msg[MAX_MSG_SIZE];
			int len;
			
			RcvMsg(&sender, msg, &len, true);

			char myMesg[] = "I am your child";
			int error = SendMsg(sender, myMesg, 16, true);
			exit(0);
		}
		else {
			char mesg[] = "I am your father";
			SendMsg(childPID, mesg, 17, true);
		}
	}

	int failed = 0;
	int msgCounter;
	int num_mesg;
	ManageMailbox(false, &num_mesg);
	for(msgCounter = 0; msgCounter < CHILD_NUM; msgCounter++) {
		pid_t aSender;
		char *reply[MAX_MSG_SIZE];
		int mLen;
		
		if(RcvMsg(&aSender, reply, &mLen, true)) {
			failed++;
		}
	}
	int status;
	return (failed == 0);
}
コード例 #9
0
int recieve_messages_even_after_stopped() {
    pid_t me = getpid(), you;
    int i = 15, j, k, len, error;

    error = SendMsg(me, &i, sizeof(int), NO_BLOCK);
    expect_true(error == 0);

    error = ManageMailbox(true, &j);
    expect_true(error == 0);
    expect_true(j == 1);

    error = RcvMsg(&you, &k, &len, NO_BLOCK);
    expect_true(error == 0);
    expect_true(k == i);
    expect_true(len == sizeof(int));
    expect_true(me == you);

    error = RcvMsg(&you, &k, &len, NO_BLOCK);
    expect_true(error == MAILBOX_STOPPED);

    return true;
}
コード例 #10
0
int mailbox_empty(void) {
    int i, error, len;
    pid_t sender;
    void* msg = malloc(MAX_MSG_SIZE);

    log("Emptying mailbox from previous test, there should be 32 messages\n");
    for (i = 0; i < 32; i++) {
        log("Removing message... ");
        error = RcvMsg(&sender,msg,&len, NO_BLOCK);
        if (error) {
            free(msg);
            log("Failed\n");
            return false;
        } else {
            log("Successful\n");
        }
    }

    log("Recieving another message, this should return mailbox empty. Test fails if it doesn't\n");
    error = RcvMsg(&sender,msg,&len, NO_BLOCK);
    free(msg);
    return error == MAILBOX_EMPTY;
}
コード例 #11
0
ファイル: testmailbox6.c プロジェクト: Tommzy/cs3013
int main() {
  int childCounter;
  
  // spawn a few threads
  int threadCounter;
  for (threadCounter = 0; threadCounter < THREAD_NUM; threadCounter++) {
    pthread_t newThread;
    int sleepFor = (threadCounter + 1) * 1000000;
    pthread_create(&newThread, NULL, goToSleep, (void *) sleepFor);
  }
  
  // fork enough children so that they can all send a message
  // to the parent and hold a pointer to it's mailbox
  for(childCounter = 0; childCounter < CHILD_NUM; childCounter++) {
    int childPID = fork();
    
    if(childPID == 0){
      pid_t sender;
      void *msg[128];
      int len;
      bool block = true;
      
      RcvMsg(&sender,msg,&len,block);
    
      
      printf("Message: %s\n", (char *)msg);
      char myMesg[] = "I am your child";
      if(SendMsg(sender, myMesg, 16, block)) {
	printf("Child send failed.\n");
      }
      
      return 0;
    }
    else{
      char mesg[] = "I am your father";
      if (SendMsg(childPID, mesg, 17, false)){
	printf("Send failed\n");
      }
    }
  }
  
  // the parent sleeps for a little time
  // waiting for some of it's children to get a pointer
  // to its mailbox
  // before trying to kill its own process.
  usleep(100000);
  
  pthread_exit(NULL);
}
コード例 #12
0
ファイル: NetObj.cpp プロジェクト: mildrock/Media
ADP::RspStreamOpen_t CNetObj::Open(ADP::ReqStreamOpen_t Req, InitObjInfo_t Info, S32 mSec)
{
	ADP::RspStreamOpen_t RspVal;

	RspVal.rVal = m_Sock.SetClient(Req.URL);
	if (RspVal.rVal != MW_SUCC)
	{
		DEBUG_ERROR("Connect err\n");
		return RspVal;
	}
	m_ObjInitInfo  = Info;
	m_StrUrl       = Req.URL;
	m_bResetSock   = false;
	m_bRuning      = true;
	m_bSafeDelete = false;
	m_tNetHandle   = ::CreateNormalThread(NetThreadProc, this, 256 * 1024);
	
	ADP::Msg_t Msg;
	Msg.Type = ADP::ADP_CMD_REQ_STREAM_OPEN;
	Msg.Body.ReqStreamOpen = Req; 
	Msg.RspId = m_MsgRspId++;
	RspVal.rVal = SndMsg(Msg, mSec);
	if (RspVal.rVal != MW_SUCC)
	{
		goto ERR_EXIT;
	}

	RspVal.rVal = RcvMsg(Msg, mSec);
	if (RspVal.rVal != MW_SUCC)
	{
		RspVal.rVal = NET_MSG_TIME_OUT;
		goto ERR_EXIT;
	}
	if (MW_SUCC != Msg.Body.RspStreamOpen.rVal)
	{
		Msg.Type = ADP::ADP_CMD_REQ_STREAM_OPEN;
		Msg.Body.ReqStreamClose.Token = Msg.Body.RspStreamOpen.Token;
		SndMsg(Msg, mSec);
		RspVal.rVal = Msg.Body.RspStreamOpen.rVal;
		goto ERR_EXIT;
	}
	
	return Msg.Body.RspStreamOpen;

ERR_EXIT:
	m_Sock.ClearSock();
	Stop();
	return RspVal;
}
コード例 #13
0
int msg_arg_error_invoke() {
    int error1 = ManageMailbox(false, NULL); // can't write to NULL, fail
    expect_true(error1 == MSG_ARG_ERROR);

    int error1_5 = ManageMailbox(true, NULL); // can't write to NULL, fail. Malformed command does not stop mailbox
    expect_true(error1_5 == MSG_ARG_ERROR);

    int error2 = SendMsg(getpid(),NULL, 6, NO_BLOCK); //can't read 6 from null, fail
    expect_true(error2 == MSG_ARG_ERROR);

    int error3 = SendMsg(getpid(),NULL,0,NO_BLOCK); // can read 0 from null, pass
    expect_true(error3 == 0);

    int error4 = RcvMsg(NULL, NULL, NULL, NO_BLOCK);    // can't send null to null, fail
    expect_true(error4 == MSG_ARG_ERROR);

    pid_t sender;
    char msg[MAX_MSG_SIZE];
    int len;
    int error5 = RcvMsg(NULL, msg, &len, NO_BLOCK); // can't read to null, fail
    expect_true(error5 == MSG_ARG_ERROR);

    int error6 = RcvMsg(&sender, msg, NULL, NO_BLOCK);  // can't read to null, fail
    expect_true(error6 == MSG_ARG_ERROR);

    int error7 = RcvMsg(&sender, msg, &len, NO_BLOCK);  // can read to all, pass
    expect_true(error7 == 0);
    expect_true(len == 0);

    int len2;
    SendMsg(getpid(),"Hello",6,NO_BLOCK);
    int error8 = RcvMsg(&sender, NULL, &len2, NO_BLOCK);    // can't read to null, len isn't 0, fail
    expect_true(error8 == MSG_ARG_ERROR);

    return true;
}
コード例 #14
0
int mailbox_stopped(void) {
    int i, error;
    pid_t me = getpid();
    void* msg = malloc(MAX_MSG_SIZE);

    ManageMailbox(true, &i);

    error = SendMsg(me, "Hello", 6, BLOCK);
    if (error != MAILBOX_STOPPED) {
        free(msg);
        return false;
    }

    error = RcvMsg(&me, msg, &i, BLOCK);
    free(msg);
    return (error == MAILBOX_STOPPED);
}
コード例 #15
0
int main() {
  int childCounter;
  
  // fork enough children so that they can all send a message
  // to the parent and hold a pointer to it's mailbox
  for(childCounter = 0; childCounter < CHILD_NUM; childCounter++) {
    int childPID = fork();
    
    if(childPID == 0){
      pid_t sender;
      void *msg[128];
      int len;
      bool block = true;
      
      RcvMsg(&sender,msg,&len,block);
    
      
      printf("Message: %s\n", (char *)msg);
      char myMesg[] = "I am your child";
      int error = SendMsg(sender, myMesg, 16, block);
      if(error) {
	printf("Child send failed. %d\n", error);
      }
      
      exit(0);
      return 0;
    }
    else{
      char mesg[] = "I am your father";
      if (SendMsg(childPID, mesg, 17, false)){
	printf("Send failed\n");
      }
    }
  }
  
  // the parent sleeps for a little time
  // waiting for some of it's children to get a pointer
  // to its mailbox
  // before trying to kill its own process.
  usleep(1000);
  printf("Parent dies.\n");
  return 0;
}
コード例 #16
0
ファイル: user.c プロジェクト: KWMalik/cs502
int main(int argc, char* argv[]) {
	int count = 22, myPID = getpid(), err;
	char msg[MAX_MSG_SIZE];
	int sender, length;

	if((err = ManageMailbox(false, &count))) {
		printf("ManageMailbox failure: %d\n", err);
		return 1;
	};
	if(count) {
		printf("count expected to be zero but instead found %d\n", count);
		return 2;
	}

	if((err = SendMsg(myPID, "test", 5, BLOCK))) {
		printf("SendMsg failure: %d\n", err);
		return 3;
	}

	if((err = ManageMailbox(false, &count))) {
		printf("ManageMailbox failure: %d\n", err);
		return 4;
	};
	if(!count) return 5;

	if((err = RcvMsg(&sender, &msg, &length, BLOCK))) {
		printf("RcvMsg failure: %d\n", err);
		return 6;
	}
	if(sender != myPID) return 7;
	if(strcmp(msg, "test")) return 8;
	if(length != 5) return 9;

	if((err = ManageMailbox(true, &count))) {
		printf("ManageMailbox failure: %d\n", err);
		return 10;
	};
	if(count) return 11;

	return 0;

}
コード例 #17
0
// CRASH TEST
// Cleaned up version of sample 7
// WILL CRASH KERNEL UNLESS PROPERLY HANDLED
int crash_test(void) {
	int childCounter;
	
	// fork enough children so that they can all send a message
	// to the parent and hold a pointer to it's mailbox
	for(childCounter = 0; childCounter < CHILD_NUM; childCounter++) {
		int childPID = fork();
		
		if(childPID == 0) {
			pid_t sender;
			void *msg[128];
			int len;
			bool block = true;
			
			RcvMsg(&sender,msg,&len,block);

			char myMesg[] = "I am your child";
			int error = SendMsg(sender, myMesg, 16, block);
			if(error) {
			}
			exit(0);
		}
		else{
			char mesg[] = "I am your father";
			if (SendMsg(childPID, mesg, 17, false)) {
			}
		}
	}
	
	// the parent sleeps for a little time
	// waiting for some of it's children to get a pointer
	// to its mailbox
	// before trying to kill its own process.
	usleep(10000);
	int status;
	int res = waitpid(-1, &status, 0);
	return 1;
}
コード例 #18
0
ファイル: NetObj.cpp プロジェクト: mildrock/Media
ADP::RspStreamMode_t CNetObj::SetStreamMode(ADP::ReqStreamMode_t Req, S32 mSec)
{
	ADP::RspStreamMode_t RspVal;
	ADP::Msg_t Msg;	
	Msg.Type = ADP::ADP_CMD_REQ_STREAM_MODE;
	Msg.Body.ReqStreamMode = Req; 
	Msg.RspId = m_MsgRspId++;
	RspVal.rVal = SndMsg(Msg, mSec);
	if (RspVal.rVal != MW_SUCC)
	{
		DEBUG_ERROR("Snd msg err\n");
		return RspVal;
	}

	RspVal.rVal = RcvMsg(Msg, mSec);
	if (RspVal.rVal != MW_SUCC)
	{
		RspVal.rVal = NET_MSG_TIME_OUT;
		return RspVal;
	}

	return Msg.Body.RspStreamMode;
}
コード例 #19
0
ファイル: test9.c プロジェクト: junl/school-homework
/*
 *  test9
 *
 *   A multi-process echo test for exit deadlock.
 *
 *   This test forks 50 child processes, sending a message to each as
 *   they block on receive.  The child then echoes the message back to the parent
 *   which ignores it letting the parent mailbox fill up.  Since the number
 *   of children is greater than the max queue size, some children will block
 *   on send.  The parent process then exits from main to test whether the
 *   blocked children will clean up and exit properly.
 *
 *   Any unexpected mailbox error code will cause the program to exit().
 */
int main( int argc, char **argv ) {

	pid_t parent_pid, child_pid;
	int i, rc;
	char msg[MAX_MSG_SIZE];

	TEST_INTRO( "Two Process Test" );

	parent_pid = getpid();

	for (i=0; i<NUM_CHILDREN; i++) {

		child_pid = fork();

		if (child_pid < 0) {

			printf("FATAL:: fork() failed\n");
			exit(-1);

		} else if (child_pid == 0) {	// child process

			char recv_buf[MAX_MSG_SIZE];
			int rc, recv_len;
			pid_t from_pid;

			child_pid = getpid();

			rc = RcvMsg( &from_pid, recv_buf, &recv_len, true );
			if (rc != 0) {
				printf("Child[%d] failed to receive message from parent!\n",child_pid);
				exit(-1);
			}

			rc = SendMsg( from_pid, recv_buf, recv_len, true );
			if (rc == MAILBOX_STOPPED) {
				printf("Child[%d] reports MAILBOX_STOPPED (expected)!\n", child_pid);
			} else {
				printf("Child[%d] returned %s from SendMsg()!\n",child_pid, mailbox_code(rc));
				exit(-1);
			}

			// child done
			exit(0);

		}

		snprintf( msg, MAX_MSG_SIZE, "Message from parent [%d] to child [%d]",parent_pid,child_pid );
		rc = SendMsg( child_pid, msg, strlen(msg), true );
		if (rc != 0) {
			printf("Parent failed to send message to child[%d]!\n", child_pid);
			exit(-1);
		}

	}

	// wait a little while to let the kiddies block on sending to parent
	usleep(500000);

	printf("This test passes if all the children exit cleanly!!\n");

	fflush(stdout);

	return 0;
}
コード例 #20
0
TInt CMessageQueue::SndMsg(const RMessage2& aMessage)
	{
	TInt msgSize = aMessage.GetDesLengthL(1);
	
	if ( iMq_ds.msg_cbytes + msgSize  > iMq_ds.msg_qbytes )
		 {//if Queue is full
		  if ( aMessage.Int2() & IPC_NOWAIT )
			  {
			  aMessage.Complete(EAGAIN);
			  return ETrue;
			  }
		  else
			  {
			  //put this request on the queue
			  if ( iExternal )
				{
				iWriteWaitQ.Append (aMessage);
				}
			  return EFalse;
			  }
		 }
	else
		{
		iMq_ds.msg_lspid = IpcUtils::GetPid(aMessage);
		iMq_ds.msg_qnum++;
		iMq_ds.msg_stime = IpcUtils::CurrentTimeL();
		iMq_ds.msg_cbytes += msgSize;

		//create message
		msg* tmsg = new msg;
		User::LeaveIfNull(tmsg);
		CleanupStack::PushL(tmsg);
		tmsg->msg_data = new char[msgSize];
		User::LeaveIfNull(tmsg->msg_data);
		tmsg->msg_len = msgSize - sizeof(long int); 
		tmsg->msg_next = NULL;//as this will be last message in the Q
		TPtr8 tptr((unsigned char*)tmsg->msg_data, msgSize, msgSize);
		aMessage.ReadL(1, tptr);
		CleanupStack::Pop();
		tmsg->msg_type = *(long*)tmsg->msg_data;
		//move pointer to actual data.
		tmsg->msg_data = tmsg->msg_data + sizeof ( long int);
		
		aMessage.Complete(KErrNone);
		//add message in message link list
		//if message exist mean add after last one

		if ( iMq_ds.msg_last )
			{
			iMq_ds.msg_last->msg_next = tmsg;
			iMq_ds.msg_last = tmsg;
			}
		else
			{//there is no message in the Q
			iMq_ds.msg_first = iMq_ds.msg_last = tmsg;
			}
		
//		try to satisfy existing request if any we can do.
		TInt max = iReadWaitQ.Count();
		for (TInt i = 0; i < max; i++)
			{
			iExternal = EFalse;
			if ( RcvMsg(iReadWaitQ[i]) )
				{
				iReadWaitQ.Remove(i);
				}
			}
		return ETrue;
		}
	}
コード例 #21
0
ファイル: testmailbox3.c プロジェクト: mracine/KernelMailbox
int main() {
  int childCounter;
  pid_t mypid;
  
  for(childCounter = 0; childCounter < CHILD_NUM; childCounter++) {
    int childPID = fork();
    
    if(childPID == 0){
      pid_t sender;
      void *msg[128];
      int len;
      bool block = true;
      
      RcvMsg(&sender,msg,&len,block);   
      
      printf("Parent %d, Message: %s\n", sender, (char *)msg);
      char myMesg[] = "I am your child";
      if(SendMsg(sender, myMesg, 16, block)) {
        printf("Child send failed.\n");
      }
      
      return 0;
    }

    else{
      char mesg[] = "I am your father";
      if (SendMsg(childPID, mesg, 17, false)){
        printf("Send failed\n");
      }

      wait(&childPID);
    }
  }
  
  usleep(3000000);
  printf("Parent awoke from sleep.\n");
  ManageMailbox(true, &childCounter);
  printf("Mailbox stopped.\n");
  
  // retrieve all enqueued messages
  while( childCounter > 0) {
    pid_t aSender;
    void *reply[128];
    int mLen;
    RcvMsg(&aSender, reply, &mLen, true);
    printf("Child %d, dequeueing # %d Message: %s\n", aSender, childCounter, (char *)reply);
    childCounter--;
  }
  
  // attempt to retrieve from and empty and stopped mailbox
  pid_t aSender2;
  void *reply2[128];
  int mLen2;
  RcvMsg(&aSender2, reply2, &mLen2, true);
  
  // atempt to send a message to a stopped mailbox
  mypid = getpid();
  int childPID2 = fork();
  if(childPID2 == 0){
    pid_t sender3;
    void *msg3[128];
    int len3;
    bool block = true;
    RcvMsg(&sender3,msg3,&len3,block);
    printf("Message: %s\n", (char *)msg3);
    char myMesg2[] = "I am your child";
    printf("PID = %d\n", mypid);
    if(SendMsg(mypid, myMesg2, 16, block)) {
      printf("Child send failed.\n");
    }
  }
  else{
    char mesg3[] = "I am your father";
    if (SendMsg(childPID2, mesg3, 17, false)){
      printf("Send failed\n");
    }

    wait(&childPID2);
  }
  
  return 0;
}
コード例 #22
0
int main (){
	char mesg[] = "This is a test";
	int mypid = getpid();
	int ret;
	int count = 0;

	/*******************************test1************************************/
	printf("\n\n###TEST1###\n");
	printf("Sending Message to pid = -3, expect MAILBOX_INVALID (1004)\n");
	ret = SendMsg(-3, mesg, 15, false);
	if (ret){
		printf("Send failed: error = %d\n", ret);
		if (ret == 1004) count++;
	}
	/*******************************test2************************************/
	printf("\n\n###TEST2###\n");
	printf("Sending message to my child (which does not exist) expect MAILBOX_INVALID (1004)\n");
	ret = SendMsg(mypid+1, mesg, 15, false);
	if (ret){
		printf("Send failed: error = %d\n", ret);
		if (ret == 1004) count++;
	}
	/*******************************test3************************************/
	printf("\n\n###TEST3###\n");
	printf("Sending message to kernel task (pid == 1) expect MAILBOX_INVALID (1004)\n");
	ret = SendMsg(1, mesg, 15, false);
	if (ret){
		printf("Send failed: error = %d\n", ret);
		if (ret == 1004) count++;
	}
	/*******************************test4************************************/
	printf("\n\n###TEST4###\n");
	printf("Try to receive message from a empty mailbox, expect MAILBOX_EMPTY (1002)\n");
	printf("Sending Message to myself.\n");
	ret = SendMsg(mypid, mesg, 15, false);
	if (ret){
	  printf("Send failed: error = %d, mypid = %d", ret, mypid);
	}
	void *msg[128];
    int len;
    bool block = true;
	int sender;
    ret = RcvMsg(&sender,msg,&len,block);
	if (ret){
		printf("Receive failed for the first time! error = %d\n", ret);
	}
    else{
		printf("Message received.\n");
		printf("Message: %s, sender = %d, len = %d, mypid = %d return = %d\n", (char *) msg, sender, len, mypid, ret);
	}
	printf("Now try receive message again, should get error MAILBOX_EMPTY (1002)\n");
	ret = RcvMsg(&sender,msg,&len,false);
	if (ret) {
		printf("Receive failed for the second time! error = %d\n", ret);
		if (ret == MAILBOX_EMPTY) count++;
	}
	
	/*******************************test5************************************/
	printf("\n\n###TEST5###\n");
	printf("Try sending a null message to myself\n");
	printf("Expect error MSG_ARG_ERROR (1006)\n");
	
	ret = SendMsg(mypid, NULL, 15, false);

	if (ret) {
		printf("Send failed: error = %d, mypid = %d\n", ret, mypid);
		if (ret == MSG_ARG_ERROR) count++;
	}
	else {
		printf("Message received.\n");
		printf("Message: %s, sender = %d, len = %d, mypid = %d return = %d\n", (char *) msg, sender, len, mypid, ret);	
	}

	/*******************************test6************************************/
	printf("\n\n###TEST6###\n");
	printf("Try receiving message from a mailbox that has been stopped, expect error MAILBOX_STOPPED (1003)\n");

	printf("Sending Message to myself.\n");
	ret = SendMsg(mypid, mesg, 15, false);
	if (ret){
	  printf("Send failed: error = %d, mypid = %d", ret, mypid);
	}
	int msgCount;
	//now stop mailbox
	ManageMailbox(true, &msgCount);
	printf("Mailbox stopped.\n");
	printf("There are %d messages in the mailbox", msgCount);
        ret = RcvMsg(&sender,msg,&len,false);
	printf("Try recieve message\n");
	printf("Should recieve message from non-empty stopped mailbox\n");
	if (ret){
		printf("Receive failed! error = %d\n", ret);
	}
        else{
		printf("Message received.\n");
		printf("Message: %s, sender = %d, len = %d, mypid = %d return = %d\n", (char *) msg, sender, len, mypid, ret);	
	}
	ret = RcvMsg(&sender,msg,&len,false);
	printf("Try recieve message again\n");
	printf("Should not recieve message from empty stopped mailbox\n");
	if (ret) {
		printf("Receive failed! error = %d\n", ret);
		count++;
	}
        else{
		printf("Message received.\n");
		printf("Message: %s, sender = %d, len = %d, mypid = %d return = %d\n", (char *) msg, sender, len, mypid, ret);
	}

	/*******************************test7************************************/
	printf("\n\n###TEST7###\n");
	printf("Try sending message to a mailbox that has been stopped, expect error MAILBOX_STOPPED (1003)\n");
	printf("Now try send message to myself again, my mailbox is stopped so should get error\n");
	ret = SendMsg(mypid, mesg, 15, false);
	if (ret){
		printf("Send failed: error = %d, mypid = %d\n", ret, mypid);
		if (ret == MAILBOX_STOPPED) count++;
	}
	else{
		printf("Message Sent.\n");
	}

	/*******************************test8************************************/
	printf("\n\n###TEST8###\n");
	printf("Try sending a message to myself with negative length, expect error MSG_LENGTH_ERROR (1005)\n");
	ret = SendMsg(mypid, mesg, -3, false);
	if (ret) {
		printf("Send failed: error = %d, mypid = %d\n", ret, mypid);
		if (ret == MSG_LENGTH_ERROR) count++;
	}
	else {
		printf("Message received.\n");
		printf("Message: %s, sender = %d, len = %d, mypid = %d return = %d\n", (char *) msg, sender, len, mypid, ret);	
	}

	/*******************************test9************************************/
	printf("\n\n###TEST9###\n");
	printf("Try sending a message to myself with length greater than max, expect error MSG_LENGTH_ERROR (1005)\n");
	ret = SendMsg(mypid, mesg, 200, false);
	if (ret) {
		printf("Send failed: error = %d, mypid = %d\n", ret, mypid);
		if (ret == MSG_LENGTH_ERROR) count++;
	}
	else {
		printf("Message received.\n");
		printf("Message: %s, sender = %d, len = %d, mypid = %d return = %d\n", (char *) msg, sender, len, mypid, ret);	
	}

	printf("%d/9 tests passed\n", count);
	return 0;
}