示例#1
0
int main(int argc, char *argv[])
{
        int child_status;
        pid_t pid;
        mqd_t qdes;
        mqd_t qdes2;
        char  qname2[] = "/mb_control";
        char  qname[] = "/mailboxLab5"; //queue name must start with '/'
        mode_t mode = S_IRUSR | S_IWUSR;
        struct mq_attr attr;
 //       int i = 0;
//        int ii=0;
        int j;
        int a[50];

        if ( argc !=5 ) {
		printf("You have to enter: ./produce <N> <B>\n");
                printf("<N> = number of integers the producer should produce\n");
		printf("<B> = number of integers the message queue can hold\n");
                printf("<P> = number of producers\n");
		printf("<C> = number of consumers\n");
                exit(1);
        }
       
        attr.mq_maxmsg  = atoi(argv[2]); //Receiving "b" from producer
        attr.mq_msgsize = sizeof(int); // The size of each message 
        attr.mq_flags   = 0;    // a blocking queue  

        qdes  = mq_open(qname, O_RDWR | O_CREAT, mode, &attr); //Opening the mailboxLab5 
        if (qdes == -1 ) {
                perror("mq_open()");
                printf("mq error inside MAIN  %d\n", getpid());
                exit(1);
        }
 
        qdes2  = mq_open(qname2, O_RDWR | O_CREAT, mode, &attr); //Opening the control mailbox - mb_control 
        if (qdes2 == -1 ) {
                perror("mq_open()");
                printf("mq error inside MAIN  %d\n", getpid());
                exit(1);
        }
         
           int msg_null = 0;
        for(j=0; j<atoi(argv[2]); j++){
           if(mq_send(qdes2, (char*)&msg_null, sizeof(int), 0) == -1){
              sleep(1);
              perror("mq_send() failed");
          } 
        }

        gettimeofday(&tv, NULL);
        t1 = tv.tv_sec + tv.tv_usec/1000000.0; //Time before creating first process
       
        for(j=0; j<atoi(argv[3]);j++) { //loops till the number of producers
          a[j]=j;
          if(spawn2("./producer.out", argv[1], argv[2],argv[3],a[j]) != 0) {
            continue; // This is to continue doing the fork when it is the parent process
          }
        }

        for(j=0; j<atoi(argv[4]);j++) { //loops till the number of consumers
          a[j+atoi(argv[3])]=j;
          if(spawn("./consumer.out", argv[1], argv[2],argv[4],a[j+atoi(argv[3])],argv[3]) != 0) {
            continue; // This is to continue doing the fork when it is the parent process
          }
        }
       
       //Calling waitpid. So the main function can wait all the processes finish, and then it can finish
       for(j=0; j<(atoi(argv[3]) + atoi(argv[4])); j++){ 
           waitpid(ch_pid[j],&child_status,0);
           if(WIFEXITED(child_status)){
           } else  
             printf("%d was  NOT waited  %d\n",j,ch_pid[j]);
       }

        if (mq_close(qdes) == -1) {
                perror("mq_close() failed inside MAIN");
                exit(2);
        }

        if (mq_close(qdes2) == -1) {
                perror("mq_close() failed inside MAIN");
                exit(2);
        }
        if (mq_unlink(qname) != 0) { 
            perror("mq_unlink() failed inside MAIN");
                exit(3);
        }
        if (mq_unlink(qname2) != 0) {                             
            perror("mq_unlink() failed inside MAIN");
                exit(3);
        }
        
        gettimeofday(&tv, NULL);
        t2 = tv.tv_sec + tv.tv_usec/1000000.0; //Time after last integer is consumed
        printf("System execution time: %f seconds\n", t2-t1); //Calculating the system execution time
        return 0;
}
示例#2
0
/* GameBored contstructor
 * @param QWidget is the parent widget
*/
GameBored::GameBored(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GameBored)
{
    ui->setupUi(this);

    // make scene
    QGraphicsScene * scene= new QGraphicsScene();
    scene->setSceneRect(0,0,800,600);
    scene->setBackgroundBrush(QBrush(QImage(":/pics/bg")));


    // make and add item to scene
    MyRect * player = new MyRect();
    scene->addItem(player);

    // focus item so that it can receive keyboard events
    player->setFlag(QGraphicsItem::ItemIsFocusable);
    player->setFocus();


    // add scene to new view
    QGraphicsView * view = new QGraphicsView(scene);

    //turn off scrollbars
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


    Board= new QWidget;
    //Board->setFixedSize(400,400);

    // add view to new layout
    // add layout to central widget
    QVBoxLayout * vlayout = new QVBoxLayout(Board);
    vlayout->addWidget(view);
    this->setLayout(vlayout);

    //
    this->show();
    view->setFixedSize(800,600);
    scene->setSceneRect(0,0,800,600);

    player->setPos(view->width()/2 -player->pixmap().width()/2, -28 /*view->height()- player->pixmap().height()*/);

    // constant creation of enemies
    QTimer* timer = new QTimer();
    QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
    timer->start(2400);

    QTimer* timer2 = new QTimer();
    QObject::connect(timer2,SIGNAL(timeout()),player,SLOT(spawn2()));
    timer2->start(5000);


    // play music
    mp3player = new QMediaPlayer();
    mp3player->setMedia(QUrl("qrc:/audio/starfox.mp3"));
    mp3player->play();

    // add score class to screen
    score = new Score();
    scene->addItem(score);
}