小编use*_*752的帖子

Unix c程序递归列出目录

我正在进行POSIX C学习练习,涉及递归列出指定目录中的文件/文件夹.该程序作为一个或多个目录的参数.我可以列出初始目录的内容很好,但有一个递归问题.我在递归函数调用的参数中传递的方式有问题吗?

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>

void listdir(char *argv[])
{
  DIR *mydirhandle;

  struct dirent *mydirent;

  struct stat statinfo;

  int n = 1;

  while(argv[n] != NULL)
  {
    if((mydirhandle = opendir(argv[n])) == NULL)
    {
      perror("opendir");
      exit(1);
    }

    printf("%s/\n", argv[n]);

    while((mydirent = readdir(mydirhandle)) != NULL)
    { 
      if((strcmp(mydirent->d_name, ".") == 0) || (strcmp(mydirent->d_name, "..") == 0))

      {
        continue;
      }

      else           
      {
        printf("\t%s\n", mydirent->d_name);

         //check if next entry is a directory …
Run Code Online (Sandbox Code Playgroud)

c unix posix

3
推荐指数
1
解决办法
4968
查看次数

将 python 2 代码转换为 python 3

我正在尝试将以下用 python 2 编写的代码转换为 python 3。此 python 代码执行 TCP 端口转发。它来自这个页面:http : //code.activestate.com/recipes/483730-port-forwarding/

import socket
import sys
import thread

def main(setup, error):
    sys.stderr = file(error, 'a')
    for settings in parse(setup):
        thread.start_new_thread(server, settings)
    lock = thread.allocate_lock()
    lock.acquire()
    lock.acquire()

def parse(setup):
    settings = list()
    for line in file(setup):
        parts = line.split()
        settings.append((parts[0], int(parts[1]), int(parts[2])))
    return settings

def server(*settings):
    try:
        dock_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        dock_socket.bind(('', settings[2]))
        dock_socket.listen(5)
        while True:
            client_socket = dock_socket.accept()[0]
            server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server_socket.connect((settings[0], settings[1]))
            thread.start_new_thread(forward, (client_socket, server_socket))
            thread.start_new_thread(forward, (server_socket, …
Run Code Online (Sandbox Code Playgroud)

python sockets multithreading python-2.7 python-3.x

3
推荐指数
1
解决办法
2740
查看次数

posix线程(pthread_create和pthread_join)

我正在努力学习Unix C并做一些练习练习.我正在处理的当前问题涉及POSIX线程(主要是pthread_create()和pthread_join())

该问题要求使用两个线程重复打印"Hello World".一个线程是打印"Hello"1000次,而第二个线程打印"World"1000次.主程序/线程是在继续之前等待两个线程完成.

这就是我现在所拥有的.

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

void *print_hello(void *arg)
{
  int iCount;
  for(iCount = 0; iCount < 1000; iCount++)
  {
     printf("Hello\n");
  }
}

void *print_world(void *arg)
{
   int iCount;
   for(iCount = 0; iCount < 1000; iCount++)
   {
      printf("World\n");
   }
}

int main(void)
{
  /* int status; */
  pthread_t thread1;
  pthread_t thread2;

  pthread_create(&thread1, NULL, print_hello, (void*)0);
  pthread_create(&thread2, NULL, print_world, (void*)0);

  pthread_join(thread1, NULL);
  pthread_join(thread2, NULL);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这似乎没有完全发挥作用.它按预期打印"Hello".但"世界"根本没有印刷.好像第二个线程根本没有运行.不确定我是否正确使用pthread_join.我的目的是让主线程"等待"这两个线程,因为练习要求.

任何帮助,将不胜感激.

c unix multithreading posix

0
推荐指数
1
解决办法
1464
查看次数

递归算法,用于查找数组中的最小元素

我有一些伪代码用于递归算法,该算法可以找到数组中的最小数字.

这是算法.

Min(A[0..n - 1])
If n = 1 return A[0]
else
{ 
  temp <-- Min(A[0..n - 2])
  if temp <= A[n - 1]
     return temp
  else return A[n - 1]
}
Run Code Online (Sandbox Code Playgroud)

我不理解这个伪代码的一部分是"temp < - Min(A [0..n - 2])"行.具体为什么在递归调用中它是"n-2"而不是"n-1"?

我的另一个问题是如何在代码中实现该行.我正在使用Java.

在此先感谢您的帮助.

java recursion pseudocode

0
推荐指数
1
解决办法
5865
查看次数