您的位置:首页 > 编程语言 > Go语言

返回值问题及goto问题

2014-06-06 01:59 225 查看
应用调用驱动时返回时,int do_sys_poll()返回类型是int ,其返回值fdcount应该为1,但是并没有return语句,所以做这个测试(对do_sys_poll中的goto理解有误)

gcc版本:gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)

ubuntu版本:ubuntu 9.10

printchar返回类型为int,但是函数没有return语句

hello.c:

#include<stdio.h>
int printchar(void)
{
printf("aa\n");
}
int main()
{
int ret=0;
ret = printchar();
printf("ret = %d\n", ret);
return 0;
}


cc hello.c
root@book-desktop:/home/book# ./a.out
aa
ret = 3


增加printchar中的a数目,然后编译执行,其打印如下:

root@book-desktop:/home/book# cc hello.c
root@book-desktop:/home/book# ./a.out
aaaaaaaaaa
ret = 11
root@book-desktop:/home/book# vim hello.c
root@book-desktop:/home/book# cc hello.c
root@book-desktop:/home/book# ./a.out
aaaaaaaaaaa
ret = 12


hello.c

#include<stdio.h>
int main()
{
int ret[3]={0,0,0}, i;
int a=0;
for(i=0; i<3; i++)
if(ret[i]>0)
goto out;
a= 3;
out:
printf("a = %d\n", a);
return 5;
return a;
}


运行结果:

root@book-desktop:/home/book# cc hello.c
root@book-desktop:/home/book# ./a.out
a = 3
root@book-desktop:/home/book# echo $?
5


由程序及运行结果可知,goto out语句没有执行,循环完了后,执行a=3, printf("a = %d", a); return 5;所以说out这段代码goto out可以用,goto out不执行(数组中没有大于0的数)时即数组查询完毕后执行执行a=3和out代码printf("a = %d", a); return 5;

do_sys_poll函数实现源码,此与forth_drv实验poll机制有关:

int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
{
struct poll_wqueues table;
int fdcount, err;
unsigned int i;
struct poll_list *head;
struct poll_list *walk;
/* Allocate small arguments on the stack to save memory and be
faster - use long to make sure the buffer is aligned properly
on 64 bit archs to avoid unaligned access */
long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
struct poll_list *stack_pp = NULL;

/* Do a sanity check on nfds ... */
if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
return -EINVAL;

poll_initwait(&table);

head = NULL;
walk = NULL;
i = nfds;
err = -ENOMEM;
while(i!=0) {
struct poll_list *pp;
int num, size;
if (stack_pp == NULL)
num = N_STACK_PPS;
else
num = POLLFD_PER_PAGE;
if (num > i)
num = i;
size = sizeof(struct poll_list) + sizeof(struct pollfd)*num;
if (!stack_pp)
stack_pp = pp = (struct poll_list *)stack_pps;
else {
pp = kmalloc(size, GFP_KERNEL);
if (!pp)
goto out_fds;
}
pp->next=NULL;
pp->len = num;
if (head == NULL)
head = pp;
else
walk->next = pp;

walk = pp;
if (copy_from_user(pp->entries, ufds + nfds-i,
sizeof(struct pollfd)*num)) {
err = -EFAULT;
goto out_fds;
}
i -= pp->len;
}

fdcount = do_poll(nfds, head, &table, timeout);

/* OK, now copy the revents fields back to user space. */
walk = head;
err = -EFAULT;
while(walk != NULL) {
struct pollfd *fds = walk->entries;
int j;

for (j=0; j < walk->len; j++, ufds++) {
if(__put_user(fds[j].revents, &ufds->revents))
goto out_fds;
}
walk = walk->next;
}
err = fdcount;
if (!fdcount && signal_pending(current))
err = -EINTR;
out_fds:
walk = head;
while(walk!=NULL) {
struct poll_list *pp = walk->next;
if (walk != stack_pp)
kfree(walk);
walk = pp;
}
poll_freewait(&table);
return err;
}

若do_poll调用完后返回值为1即fdcount =1,若goto out_fds的while,if条件满足,就直接跳到out_fds标号处,最后return err相当于 return -EFAULT,若while if的条件不满足不执行goto out_fds,则err = fdcount =1,由于fdcount
=1,则if条件为if(0)

if (!fdcount && signal_pending(current))
err = -EINTR;
然后执行out_fds段代码,此时return err相当于return 1即do_sys_poll的返回值为1

poll机制留待下回分析
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: