1111// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1212// ==============================================================================================
1313
14+ #include < unistd.h>
15+
1416#include " hittable.h"
1517#include " material.h"
18+ #include < sys/mman.h>
19+ #include < sys/wait.h>
1620
1721
1822class camera {
@@ -35,9 +39,37 @@ class camera {
3539
3640 std::cout << " P3\n " << image_width << ' ' << image_height << " \n 255\n " ;
3741
42+ int core_count = 6 ;
43+ int image_size_in_bytes = sizeof (color) * image_width * image_height;
44+ color *rendered_image = (color *) mmap (nullptr , image_size_in_bytes,
45+ PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1 , 0 );
46+
3847 for (int j = 0 ; j < image_height; j++) {
3948 std::clog << " \r Scanlines remaining: " << (image_height - j) << ' ' << std::flush;
40- render_line (j, world);
49+
50+ // Start to create the child proccesses
51+ for (int i = 0 ; i < core_count; i++) {
52+ pid_t pid = fork ();
53+ if (pid == -1 ) {
54+ // Error
55+ printf (" Error forking process\n " );
56+ exit (EXIT_FAILURE);
57+ }
58+ else if (pid == 0 ) {
59+ // Child proccess
60+ printf (" Created child process number: %d\n " , i);
61+ int start_j = j * (image_height / core_count);
62+ render_line (start_j, world, rendered_image);
63+ printf (" process %d\n finished" , i);
64+ exit (0 );
65+ }
66+ }
67+
68+ // Write the calculated pixels
69+ for (int i = 0 ; i < image_size_in_bytes; i++) {
70+ color color = rendered_image[i];
71+ write_color (std::cout, color);
72+ }
4173 }
4274
4375 std::clog << " \r Done. \n " ;
@@ -54,14 +86,15 @@ class camera {
5486 vec3 defocus_disk_u; // Defocus disk horizontal radius
5587 vec3 defocus_disk_v; // Defocus disk vertical radius
5688
57- void render_line (int j, const hittable& world) {
89+ void render_line (int j, const hittable& world, color* rendered_image ) {
5890 for (int i = 0 ; i < image_width; i++) {
5991 color pixel_color (0 ,0 ,0 );
6092 for (int sample = 0 ; sample < samples_per_pixel; sample++) {
6193 ray r = get_ray (i, j);
6294 pixel_color += ray_color (r, max_depth, world);
6395 }
64- write_color (std::cout, pixel_samples_scale * pixel_color);
96+ int pixel_memmory_adress = (j+1 )+(i+1 );
97+ rendered_image[pixel_memmory_adress] = pixel_color * pixel_samples_scale;
6598 }
6699 }
67100
0 commit comments