⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pthreads_and_signals.c

📁 Linux 环境下 C 开发的初始练习代码
💻 C
字号:
                                                                                                                                                                                       // file
 
//pthreads_and_signals.c
// Study of


#include <ctype.h>
#include <fcntl.h>
#include <math.h>
#include <pthread.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

typedef unsigned short int16;
typedef unsigned char  int8;
typedef unsigned long  int32;

typedef volatile unsigned char  vint8;
typedef volatile unsigned short vint16;
typedef volatile unsigned long  vint32;

pthread_t thread;

int PID;
int interrupt;

typedef struct {
  int a;
  int b;
} junk_t;
junk_t some__junk;

int32 c1, c2;

void
 pthread(junk_t);
typedef void (*sighandler_t)(int);
sighandler_t handler(int);

//------------------------------------------------------------------------------

sighandler_t handler(int s) {

  int32   c1_old = c1;

  printf(" start signal handler, s=%d\n", s);
  printf("PID=%d getpid()=%d\n", PID, getpid());
  for (c2=0; c2<100000000; c2++) {
  }
  printf("c1_old=%lu c1=%lu delta=%lu c2=%lu\n", c1_old, c1, (int32)(c1-c1_old), c2);
  interrupt = 0;
}

//------------------------------------------------------------------------------

void
 pthread(junk_t junk) {

  while (1) {
    printf("from parallel thread: start sleep(1)\n");
    sleep(1);
    if (!interrupt) {
      printf("from parallel thread: end sleep, will signal now\n");
      if (kill(PID, SIGUSR1) == -1) perror("kill():");
      else                     interrupt = 1;
    }
  }
}

//------------------------------------------------------------------------------

int main(void) {

  printf("Hello World\n");

  PID = getpid();
  interrupt = 0;

  if (signal(SIGUSR1, (sighandler_t)handler) == SIG_ERR) {
    printf("signal(1...) failed.\n");
    return 0;
  }

// Create Thread

  some__junk.a = 123;

  if (pthread_create(&thread, NULL, (void*)pthread, &some__junk)) {
    printf("BU_ERROR_LINUX_IRQ_INSTALL_FAILED\n");
    return 0;
  }
  while (1) {
    c1 = c1 + 1;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -