Tag: c

餐饮哲学家饥饿的可能性

如果它保证满足以下所有条件,我需要检查我解决餐饮哲学家问题的算法: 没有僵局的可能性。 没有饥饿的可能性。 我在筷子上使用信号量来解决问题。 这是我的代码(算法): while(true) { // He is Hungry pickup_chopsticks(i); // He is Eating… drop_chopsticks(i); // He is thinking } // … void pickup_chopsticks(int i) { if(i % 2 == 0) /* Even number: Left, then right */ { semaphore_wait(chopstick[(i+1) % NUM_PHILOSOPHERS]); semaphore_wait(chopstick[i]); } else /* Odd number: Right, then left */ { […]

基准测试代码 – 我做得对吗?

我想对C / C ++代码进行基准测试。 我想测量cpu时间,挂起时间和周期/字节。 我写了一些测量函数但是有周期/字节的问题。 为了得到一个cpu时间,我用RUSAGE_SELF编写了一个函数getrusage() ,对于墙上时间我使用带有RUSAGE_SELF clock_gettime ,得到周期/字节我使用rdtsc 。 我处理一个大小的输入缓冲区,例如1024: char buffer[1024] 。 我如何基准测试: 做一个热身阶段,只需调用fun2measure(args) 1000次: for(int i=0; i<1000; i++) fun2measure(args); 然后,做一个实时基准测试,为墙上时间: `unsigned long i; 双倍时间; double timeTotal = 3.0; //处理3秒 for(timeTaken =(double)0,i = 0; timeTaken <= timeTotal; timeTaken = walltime(1),i ++)fun2measure(args); ` 而对于cpu时间(几乎相同): for (timeTaken=(double)0, i=0; timeTaken <= timeTotal; timeTaken = walltime(1), […]

GCC 5.1循环展开

给出以下代码 #include int main(int argc, char **argv) { int k = 0; for( k = 0; k < 20; ++k ) { printf( "%d\n", k ) ; } } 使用GCC 5.1或更高版本 -xc -std=c99 -O3 -funroll-all-loops –param max-completely-peeled-insns=1000 –param max-completely-peel-times=10000 部分循环展开,它将循环展开十次然后进行条件跳转。 .LC0: .string “%d\n” main: pushq %rbx xorl %ebx, %ebx .L2: movl %ebx, %esi movl $.LC0, […]

使用mingw在套接字上的fprintf

在Windows中的一个套接字上有一个有趣的post使用fprintf建议用一个包装器替换fprintf,该包装器将一个格式缓冲区发送到套接字。 但是,似乎可以使用_open_osfhandle将套接字转换为filedescriptor。 #include #include #include #include int main(int argc, char* argv[]) { if (argc < 3) { fprintf(stderr,"usage %s \n”, argv[0]); exit(0); } WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); int port = atoi(argv[2]); struct hostent *server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,”ERROR, no such host\n”); exit(0); } struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(struct sockaddr_in)); […]

使用c进行OpenMP优化

我应该优化下面的代码,使其使用openMP和内存阻塞运行至少16倍。 到目前为止,我只能想到使用下面的简单语句来折叠for循环。 这使它运行速度提高了3倍。 有什么想让它接近16? int i,j; #pragma omp parallel for collapse(2) //my inserted code for (i = 0; i < MSIZE; i++) for (j = 0; j < MSIZE; j++) d[i][j] = c[j][i];

我能得到的最简单的getopt程序?

在关于如何使用getopt() 链接上做了一些阅读之后,我试图得到一个小例子。 我想要的是: ./prog -v # show me prog version ./prog -f filename # just show me the filename I entered from the command line 这是我到目前为止写的: #include #include #include int main(int argc, *argv[]) { char VER[] = “0.1.1”; int opt; opt = getopt(argc, argv, “vf:”); char *filename; while (opt != -1) { switch(opt) { case […]

`f(void)`表示C ++ 11或C中没有参数?

在C ++ 11中,以下函数声明: int f(void); 意思是: int f(); 由非依赖类型void的单个未命名参数组成的参数列表等效于空参数列表。 我得到(也许是错误的)印象这是一个旧function,也许是从Cinheritance而来的? 有没有人知道这种方式背后的历史或理由来声明一个没有参数的函数?

C警告冲突类型

我的代码是 void doc(){ //mycode return; } 我的警告是 conflicting types for ‘doc’ 任何人都可以解决它。

代码在C中没有按预期工作

我正在用C编写一个程序来计算句子中的空格数。 但我还没有设法让它正常工作。 如果我输入类似Hello world 1234的内容 ,当预期输出为5时,我得到的输出是3。 我的代码是: //Program to count number of words in a given Sentence #include #include int main() { char sent[100]; char sentence[] = {‘ ‘, ‘\0’}; printf(“\nEnter a sentence :\n”); gets(sent); strcat(sentence, sent); int l = strlen(sentence), i = 0, count = 0, countCh = 0; printf(“%d”, l); char ch, ch1; […]

%x和〜的重要性

int m=32 printf(“%x” , ~m); 这个语句的输出是ffdf而没有~ output是20 。 %x和~的意义是什么?