shithub: riscv

Download patch

ref: c04297587cb045b010f6c2f642b1079d336ff27b
parent: c31c4ca7b37ae113e9df810f03157c2fe7bb2708
author: cinap_lenrek <[email protected]>
date: Sat Apr 20 18:47:39 EDT 2013

ip/torrent: use NPROC processes in parallel for calculating initial piece hashes

--- a/sys/src/cmd/ip/torrent.c
+++ b/sys/src/cmd/ip/torrent.c
@@ -49,6 +49,7 @@
 };
 
 int debug;
+int nproc = 1;
 int killgroup = -1;
 int port = 6881;
 char *deftrack = "http://exodus.desync.com/announce";
@@ -1197,6 +1198,12 @@
 		usage();
 	} ARGEND;
 
+	if((s = getenv("NPROC")) != 0){
+		if((nproc = atoi(s)) <= 0)
+			nproc = 1;
+		free(s);
+	}
+
 	fd = 0;
 	if(*argv)
 		if((fd = open(*argv, OREAD)) < 0)
@@ -1301,8 +1308,18 @@
 	if(len)
 		sysfatal("pieces do not match file length");
 
-	for(i = 0; i<npieces; i++)
-		havepiece(i);
+	for(i=0; i<nproc; i++){
+		switch(rfork(RFPROC|RFMEM)){
+		case -1:
+			sysfatal("fork: %r");
+		case 0:
+			for(; i<npieces; i+=nproc)
+				havepiece(i);
+			exits(0);
+		}
+	}
+	while(waitpid() >= 0)
+		;
 
 	srand(time(0));
 	atnotify(catch, 1);