以下程序测试英特尔处理器上的Little/Big endian.实际上小端是正确的输出.首先我铸造int到char*和没有初始化访问其价值int *.我是不理解输出的第二部分.这里int指针被转换为char *.那么为什么int指针没有改变它的对齐方式char *呢?
00000000 00000000 00000011 01111111 = 895
0 0 3 127
int main() {
int num = 895;
if(*(char *)&num == 127)
{
printf("\nLittle-Endian\n");
}
else
{
printf("Big-Endian\n");
}
int *p = (char *)&num ;
if(*p == 127)
{
printf("\nLittle-Endian\n");
}
else
{
printf("Big-Endian\n");
}
printf("%d\n",*p);
}
o/p
Little-Endian
Big-Endian
895
Run Code Online (Sandbox Code Playgroud) 我无法理解以下程序的输出.我观察到在子进程返回后,父进程在wait()之前没有休眠3秒.如果SIGCHLD设置为默认处理程序,则它会休眠3秒,调用wait并按预期返回.到底发生了什么?
# include <unistd.h>
# include <sys/types.h>
# include <stdio.h>
# include <sys/wait.h>
# include <signal.h>
void handler(int sig) {
printf("Iam in handler ...\n");
}
main() {
int status;
pid_t pid;
struct sigaction act;
//act.sa_flags=SA_NOCLDSTOP;
act.sa_handler=handler;
sigaction(SIGCHLD,&act,NULL);
if(!fork()) {
printf("child process id is %d\n",getpid());
return 1;
}
printf("xxx ...\n");
sleep(3);
pid = wait(&status);
printf("process terminated is %d\n",pid);
}
output::
xxx ...
child process id is 2445
Iam in handler ...
process terminated is 2445
Run Code Online (Sandbox Code Playgroud) 我不明白read()系统是如何阻塞的.我创建了一个空文件并尝试使用read()系统调用进行读取.它返回0.
fd = open("Demo.txt",O_RDONLY);
n = read(fd,&ch,10); // returns 0
Run Code Online (Sandbox Code Playgroud)
我期待read()无限期地阻止,因为文件中没有数据.read()会将EOF视为有效数据并立即返回吗?我的理解是否正确?
在下面的代码中,我调用pthread_join(),其中线程ID为self.结果是它返回错误号35.所以同样我试图用perror打印.但它显示出"成功".我怀疑是库/系统调用是否需要明确地为任何错误设置errno或者我是否错过任何东西?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#define DEATH(mess) { perror(mess); exit(errno); }
static void * threadFunc(void *arg)
{
void *res;
printf("sleeping for 2 sec ...\n");
sleep(2);
char *s = (char *) arg;
pthread_t t = pthread_self();
int relval = pthread_join(t, &res);
if (relval)
perror("deadlock");
printf("return value is %d .....\n",relval);
return (void *) strlen(s);
}
int main(int argc, char *argv[])
{
pthread_t t1;
void *res;
int ret;
ret = pthread_create(&t1, NULL, threadFunc, "Hello world\n"); …Run Code Online (Sandbox Code Playgroud) 我正在学习RAW套接字.在下面的代码中,我试图打印所有ICMP数据包标头信息.看起来代码中有些错误.任何人都可以帮助我,我错了.
# include <unistd.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <string.h>
# include <netinet/in.h>
# include <stdio.h>
# include<stdlib.h>
main(){
int sockfd,retval,n;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
char buf[10000];
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0){
perror("sock:");
exit(1);
}
clilen = sizeof(struct sockaddr_in);
while(1){
printf(" before recvfrom\n");
n=recvfrom(sockfd,buf,10000,0,(struct sockaddr *)&cliaddr,&clilen);
printf(" rec'd %d bytes\n",n);
buf[n]='\0';
printf(" msg from client = %s\n",buf);
}
}
o/p
before recvfrom
rec'd 60 bytes
msg from client = E
before …Run Code Online (Sandbox Code Playgroud) 编译下面显示的代码行(X86)时,会生成相应的汇编指令.895是-ve数,并以%esp表示的存储位置以2的补码形式存储.
int a = -895 --> compiler ---> movl $-895, 24(%esp)
Run Code Online (Sandbox Code Playgroud)
我的疑问是,汇编程序是否直接将-895转换为2的补码形式并生成机器指令或执行CPU的ALU,同时执行相应的机器指令-895作为参数并存储在内存位置?
在简单的客户端 - 服务器程序中,客户端正在发送12个字节的数据.我正在使用recvfrom(),在连续调用中请求2和10个字节.在第一次调用的情况下,recvfrom()返回2个字节.但第二个电话是封锁.为什么会这样?我认为它不会发生TCP套接字,其中连续的读取调用返回可用字节(这里10).