Tag: 操作系统

为什么8位字段具有字节序?

请参阅/netinet/tcp.h中TCP头的定义: struct tcphdr { u_int16_t th_sport; /* source port */ u_int16_t th_dport; /* destination port */ tcp_seq th_seq; /* sequence number */ tcp_seq th_ack; /* acknowledgement number */ # if __BYTE_ORDER == __LITTLE_ENDIAN u_int8_t th_x2:4; /* (unused) */ u_int8_t th_off:4; /* data offset */ # endif # if __BYTE_ORDER == __BIG_ENDIAN u_int8_t th_off:4; /* data […]

使用fork()的子进程和父进程

我遇到了问题,我需要制作一个程序来制作9个子进程,之后我必须进行3秒的倒计时并使这9个进程等待来自父亲的信号,在他们收到这个信号后,每个孩子们应该说他是什么孩子(如果他是孩子#1,#2,#3等……,按照他们的顺序)。 我所做的就是在这里,一切都很好,我想,直到我作为一个孩子说的部分,我的号码是什么,我不知道怎么做,因为每个孩子都是不同的进程,他们不共享内存,信号不能使用参数,现在我在名为“处理程序”的函数上打印PID,但是如何打印我的号码,作为一个孩子? #include #include #include #include #include #include void handler(int x); int main() { pid_t child[9]; pid_t child_pid; for (int i = 0; i < 9; ++i) { child_pid = fork(); child[i] = child_pid; if (child_pid == 0) break; if (child_pid < 0) { perror("fork()"); exit(EXIT_FAILURE); } } if (child_pid == 0) { signal(SIGUSR1, handler); […]

Fork永远不会进入孩子的过程

我正在编写一个模仿shell行为的代码,特别是&和|。 我的函数接收用户命令,并检查是否有&在最后,然后子进程应该在后台运行,父进程不应该等待它完成并继续执行命令。 它也应该检查是否有| 在输入数组中运行两个子进程,同时管道他们的stdin和stdout。 我已经实现了&的行为,但每当我编译并运行我的代码时,我只从父进程中获取printf语句。 我想听听如何解决这个问题的想法,此外,我将不胜感激任何关于|的实施的建议 (管道)以及如何防止僵尸。 int process_arglist(int count, char** arglist) { int pid = fork(); printf(“%d”, pid); switch (pid) { case -1: fprintf(stderr, “ERROR: fork failed\n”); return 1; break; case 0: // Son’s proccess printf(“I got to son”); //check last arglist argument if (strcmp(arglist[count – 1], “&”) == 0) { setpgid(0, 0); arglist[count […]

系统标头和普通标头gcc

我知道这是一个非常愚蠢的问题,但我无法区分gcc中的系统头和普通头。 参考此链接 : 2.8系统接头 声明操作系统和运行时库接口的头文件通常不能严格符合C语言编写。因此, GCC会给出系统头文件中的代码特殊处理 。 GCC处理系统标题时,除“#warning”(参见“诊断”)生成的警告外,所有警告都将被禁止。 系统标题中定义的宏在任何扩展的地方都不受几个警告的影响。 当我们发现警告由于系统头中定义的宏中的代码而产生大量误报时,会临时授予此免疫权。 通常,只有特定目录中的标头才被视为系统标头。 这些目录是在编译GCC时确定的。 但是,有两种方法可以将普通标头添加到系统标头中: 在使用-isystem和-idirafter命令行选项添加到搜索路径的目录中找到的头文件被视为用于诊断目的的系统头。 还有一个指令#pragma GCC system_header,它告诉GCC将当前包含文件的其余部分视为系统头,无论它在哪里找到。 文件中“#pragma”之前的代码不受影响。 #pragma GCC system_header对主源文件没有影响。 我很感激答案,如果可能的话,会显示系统标题的一些内容,以及在链接中讨论的是什么样的警告或特殊处理。

由于在更改巨大页面分配的大小时initialize()函数导致的分段错误

当我使用大页面分配的内存大小时,我在程序中遇到分段错误,即,当我定义LENGTH = 4 * 1024时,存在seg错误。 当我定义4 * 1024 * 1024时,没有seg错误。 这是什么原因? 代码如下: #define _POSIX_C_SOURCE 199309 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #define PROTECTION (PROT_READ | PROT_WRITE) #define LENGTH (4*1024) //#define LENGTH (4*1024*1024) #define LINE_SIZE 64 #define ASSOC 16 #define CACHE_SIZE (4*1024*1024) #define WAY_SIZE (CACHE_SIZE/ASSOC) #ifndef MAP_HUGETLB #define MAP_HUGETLB 0x40000 #endif […]

软件用于Windows操作系统的C中断服务程序

#include #include #include void Task() { printf(“Hi”); } int main ( ) { time_t t; clock_t start, end; long i; long count; double x = 0.0; count = 2; start = clock(); time(&t); printf(ctime(&t)); printf( “Counting to %ld\n”, count ); if(count) { Task(); } end = clock(); printf( “That took %f seconds and I counted […]

如何在档案文件中打印文件名?

我是C和系统编程的新手。 我想打开一个存档文件并打印出存档文件中文件的名称(例如,我的存档文件是weds.a;在weds.a中,我有thurs.txt和fri.txt“。我想创建显示thurs.txt fri.txt的输出 编辑:它应该像ar -t命令一样工作。 有人可以给我一些如何做的提示吗? 我一直在阅读手册页并在线查找示例,但我没有在哪里。 我相信我错过了一些东西。 我下面的代码只打印链接计数。 有人可以帮忙吗? 在此先感谢您的帮助!! #include #include #include #include #include #include #include #include #include #include int main (int argc, char **argv) { int in_fd; struct stat sb; if (argc != 2) { printf(“Error”, argv[0]); exit(EXIT_FAILURE); } if (stat(argv[1], &sb) == -1) { perror(“stat”); exit(EXIT_FAILURE); //change from EXIT_SUCCESS to EXIT_FAILURE […]

如何使用信号量同步进程

假设我有3个进程,包括父进程,我必须按照P3,P1,P2的顺序执行I程序。 伙计们,请帮助我如何从流程P3开始计算。 我需要输出为{0,1,2,3,4,5,.. max} 作为参考我的代码快照是: – #define SEM_NAME “//test.mutex” //#define SEM_NAME2 “//test2.mutex” int main(int argc, char const *argv[]) { int max = 0, i =0; sem_t *sem; sem_t *sem2; pid_t pid, pid2; sem = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1); sem_unlink(SEM_NAME); if (sem==SEM_FAILED) { printf(“%s sem_open failed!”, SEM_NAME); return (-1); } // sem2 = sem_open(SEM_NAME2, O_CREAT, O_RDWR, […]

c中的简单shell:waitpid系统调用无法正常工作

我必须创建简单的shell来读取命令并按顺序执行它们。 condition不会改变main函数的forms,而execute函数应该是递归的。 主要问题是,似乎waitpid不起作用。 但我知道,我的代码中存在很多问题。 请让我知道我应该从哪里开始.. #include #include #include #include #include #include #define MAX 10 char cmmd[MAX][256]; int sp; char *argv[10]; int size; void ClearLineFromReadBuffer(void){ while(getchar() != ‘\n’); } void printCommands(){ size = sp+1; //print by moving stack pointer while(1){ if (sp==-1) break; printf(“Command line : %s\n”, cmmd[sp]); sp–; } printf(“print end\n”); } void readCommandLines(){ int […]

如何在c中将char数组设置为string

所以我有一个结构调用Process_Info struct Process_Info { char name[128]; int pid; int parent_pid; int priority; int status; }; 和一组Process_Info调用信息。 我将信息中的pid设置为一个整数,它可以工作,但是当我尝试将info中的名称设置为“{kernel}”时,就像这样 info[i].name=”{kernel}”; 它给了我不兼容的分配错误类型。 我在网上搜索似乎我可以这样做,比如http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/ ,他们做了char label [] =“Single”; 那么我做错了什么?