在C中创建倒计时器并继续代码流

时间:2014-11-19 17:32:04

标签: c

所以我在c进程中进行编程,我需要创建一个计时器,当它通过我的代码中的某个点时,它会激活x秒,然后在倒数计时器达到0之后它会做一些事情。但在此期间,定时器之后的代码,需要继续做才能工作,遵循代码流程。 所以在简历中我想要实现的是c中的倒数计时器,当它被激活时,它会创建另一个进程,如果它计算时间并在之后做一些事情。

很抱歉,如果我没有解释得这么好。

提前致谢。

到目前为止,我的代码是以下内容,我想将计时器放在consumeTutuc方法的中间。

#include "sema.h"
#include <sys/shm.h>
//#include <sys/types.h>


#define MAX_CHILD     3         /* max. # of child processes    */
#define LIFETIME      5     /* max. lifetime of a process   */
#define MAX_NTIME     4         /* max. time for normal work    */
#define MAX_CTIME     2         /* max. time for critical work  */
#define N 10  
#define SHMKEY (key_t) 0x10
#define SHMKEY2 (key_t) 0x11
#define SHMKEY3 (key_t) 0x12
#define SHMKEY4 (key_t) 0x13


//estruturas
typedef struct
{
int numeroPessoas;
char condicao;

}PESSOAS;

typedef struct
{
int id;
int espera;

}TUTUC;

int in, out, inP, outP, tt;
int *ptr; /*posicao do tuctuc*/
int *ptr2; /*numero disponiveis de tuctuc*/
PESSOAS *ptr3; /*fila de pessoas*/
int *ptr4; /*pessoas a espera*/
semaphore mutex, empty, full, emptyP, fullP, mutexP;



void produceTucTuc();
void consumeTucTuc();
void producePessoas();
void consumePessoas();


int main (void)
{
//MEMORIA
int shmid;
int shmid2;
int shmid3;
int shmid4;
char *addr;
char *addr2;
char *addr3;
char *addr4;
shmid = shmget(SHMKEY, N, 0777|IPC_CREAT);
shmid2 = shmget(SHMKEY2, N, 0777|IPC_CREAT);
shmid3 = shmget(SHMKEY3, N, 0777|IPC_CREAT);
shmid4 = shmget(SHMKEY4, N, 0777|IPC_CREAT);
addr=(char*)shmat(shmid,0,0);
addr2=(char*)shmat(shmid2,0,0);
addr3=(char*)shmat(shmid3,0,0);
addr4=(char*)shmat(shmid4,0,0);
ptr=(int*)addr;
ptr2=(int*)addr2;
ptr3=(PESSOAS*)addr3;
ptr4=(int*)addr4;
//MEMORIA

tt=1;
in=0;
out=0;
inP=0;
outP=0;
*ptr=0;
*ptr2=1;

//SEMAFOROS
full = init_sem(0);
empty = init_sem(N);
mutex = init_sem(1);
fullP = init_sem(0);
emptyP = init_sem(N);
mutexP = init_sem(1);
semaphore s;
//SEMAFOROS

  int   child_pid [MAX_CHILD],  /* process ID's         */
        wait_pid;
  int       i, j,i2,            /* loop variables       */
        child_stat;     /* return status of child   */

  srand (time(NULL));
  s = init_sem (1);         /* create sem with value 1  */

  for(i=0; i<10; i++){
    produceTucTuc();
}
  for (i = 0; i < MAX_CHILD; ++i)
  {
    child_pid [i] = fork ();

    switch (child_pid [i])
    {
      case -1:              /* error: no process created    */
    perror ("fork failed");
    exit (1);
    break;
      case 0:               /* child process        */
    /* up to Linux 2.0.x the process didn't terminate after
     * LIFETIME seconds because "alarm()" and "sleep()" have
     * used the same clock. This problem is solved in Linux 2.2.x
     */


    if(i==0) produceTucTuc();
    //if(i==1) consumeTucTuc();
    if(i==1) producePessoas();
    if(i==2) consumePessoas();


    break;
      default:              /* parent process       */
    if (i == (MAX_CHILD - 1))   /* all childs created ?     */
    {               /* yes -> wait for termination  */
      for (j = 0; j < MAX_CHILD; ++j)
      {
        wait_pid = wait (&child_stat);
        if (wait_pid == -1)
        {
          perror ("wait failed");
        };
      };
      printf ("All child processes have terminated.\n");
    //LIBERTA MEMORIA
      rel_sem (s);
    rel_sem (mutex);
    rel_sem (empty);
    rel_sem (full);
    rel_sem (mutexP);
    rel_sem (emptyP);
    rel_sem (fullP);
    shmdt(addr);
        shmctl(shmid, IPC_RMID,NULL);
    shmdt(addr2);
    shmctl(shmid2, IPC_RMID,NULL);
    shmdt(addr3);
    shmctl(shmid3, IPC_RMID,NULL);
    shmdt(addr4);
    shmctl(shmid4, IPC_RMID,NULL);

    //LIBERTA MEMORIA
    };
    };                  /* end switch           */
  };                    /* end for          */
  return 0;
}

void produceTucTuc(){
int ntuctuc;
alarm(LIFETIME);

//for(;;){
    P(empty);
    P(mutex);
    *(ptr+in)=tt;
    ntuctuc=*ptr2;
    printf("------------------------------------\n");
    printf("Produzi na pos %d o tuctuc %d\n",in,tt);
    printf("Numero de TucTucs disponiveis:%d\n",ntuctuc);
    in=(in+1)%N;
    ntuctuc++;
    *ptr2=ntuctuc;
    tt=(tt+1)%N;
    V(mutex);
    V(full);
//}
}

void consumeTucTuc(){
int tuctuc;
int ntuctuc;
alarm(LIFETIME);

    P(full);
    P(mutex);
    tuctuc=*(ptr+out);
    ntuctuc=*ptr2;
    ntuctuc--;
    //printf("------------------------------------\n");
    printf("Consumi na pos %d o tuctuc %d\n",out,tuctuc);
    printf("Numero de TucTucs disponiveis:%d\n",ntuctuc);
    out=(out+1)%N;
    *ptr2=ntuctuc;
    V(mutex);
    V(empty);

}

void producePessoas(){
    PESSOAS pessoa;
    int espera =0;
    alarm(LIFETIME);
    for(;;){
        P(emptyP);
        P(mutexP);
        if(rand()%5 == 0){
            pessoa.numeroPessoas = 5;
        }
        else {
            pessoa.numeroPessoas=rand()%5;
        }

        if(rand()%10<3){
            pessoa.condicao = 'E';
        }
        else {
            pessoa.condicao = 'N';
        }
        *(ptr3+inP)=pessoa;
        espera=*ptr4;
        espera=espera+pessoa.numeroPessoas;
        *ptr4=espera;
        printf("------------------------------------\n");
        printf("Na pos %d entraram %d pessoas com %c condicao\n",inP,pessoa.numeroPessoas, pessoa.condicao);
        printf("Estao %d pessoas à espera\n",*ptr4);
        inP=(inP+1)%N;
        V(mutexP);
        V(fullP);
    }
}

void consumePessoas(){
int pessoa;
int espera;
int p;
int aux;
alarm(LIFETIME);
while((*(ptr3 + outP)).numeroPessoas<(*ptr2)){
    P(fullP);
    P(mutexP);
    pessoa=(*(ptr3+outP)).numeroPessoas;
    espera=*ptr4;
    espera=espera-pessoa;
    *ptr4=espera;
    printf("------------------------------------\n");
    printf("Na pos %d sairam %d pessoas\n",inP,pessoa);
    printf("Estao %d pessoas à espera\n",espera);
    if(pessoa%2==1){
    aux=(pessoa/2) + 1;
    }else{
    aux=pessoa/2;
    }
    for(p=0; p<aux; p++){
    printf("%d\n",pessoa);
    consumeTucTuc();
    }
    outP=(outP+1)%N;

    V(mutexP);
    V(emptyP);
}
}

1 个答案:

答案 0 :(得分:0)

此功能可以作为线程调用,它是一个计数器:

(你可以在while循环中构建一个条件并杀死线程!)

void counter( void *dummy ){
    time_t start;
    int weiter = 1,count = 0;
    while (weiter) {
        time(&start);
        printf("\b\b\b\b%3i:",count);
        while(difftime(time(NULL),start)<1);
        count++;

        //if condition kill thread

    }

}